Java 字符串数组到格式为的字符串

Java 字符串数组到格式为的字符串,java,android,arrays,string,Java,Android,Arrays,String,我设置了3个文本视图,每个视图最多9个字符。用户可以编辑这些文本视图并将其保存到文件中。我首先将字符串数组转换为字符串 StrLabels[0]=label1.getText().toString(); StrLabels[1]=label2.getText().toString(); StrLabels[2]=label3.getText().toString(); StrFile=(StrLabels[0] + StrLabels[1] +

我设置了3个文本视图,每个视图最多9个字符。用户可以编辑这些文本视图并将其保存到文件中。我首先将字符串数组转换为字符串

   StrLabels[0]=label1.getText().toString();
   StrLabels[1]=label2.getText().toString();        
   StrLabels[2]=label3.getText().toString();    

   StrFile=(StrLabels[0] + StrLabels[1] + StrLabels[2]);

   writeToFile(StrFile);
StrFile可以很好地保存3个字符串,但我还需要反向处理并读取文件。是否有办法将每个strlabel[]与StrFile分开

谢谢Gianmarco,这是您帮助下的最终代码。我倒过来,让它加在绳子的末端,而不是前面

      String totalString = ""; // initialization of the total string
      String piece = StrLabels[0];


      String toBeAdded = "";
      if(piece.length() < 9){
      int length = piece.length();
      toBeAdded = piece;
              while(length < 9){
              toBeAdded =toBeAdded +"";
              length++;
              }
       } else if(piece.length() > 9){

       throw new IllegalArgumentException ("Error, string longer than 9");
       } else {
       toBeAdded = piece;
       }
       totalString = totalString + toBeAdded;
String totalString=”“;//总字符串的初始化
弦段=标准标签[0];
字符串tobeaded=“”;
如果(件长()<9){
int length=工件长度();
tobedded=件;
while(长度<9){
TOBEADED=TOBEADED+“”;
长度++;
}
}否则如果(件长()>9){
抛出新的IllegalArgumentException(“错误,字符串长度超过9”);
}否则{
tobedded=件;
}
totalString=totalString+Toheaded;

您可以在这些字符串中选择一些特殊字符,或者在保存它们之前添加新行字符(例如,我选择“;”)

从文件中读取后,您可以使用:

    strFile.split(";");

返回字符串数组。

例如,保存字符串时可以使用分隔符:

StrFile=(StrLabels[0]+“\n”+StrLabels[1]+“\n”+StrLabels[2])

当您从文件中读取时,只需执行以下操作:


String[]StrLabels=StrFile.split(“\n”)

一个基本的解决方案是使用分隔符,但这意味着要处理分隔符在字符串中的情况,这意味着要确保它工作非常麻烦。一个更简单的解决方案是依靠现有的方法

JSON 只需创建一个
JSONArray
,将字符串放入其中,在
JSONArray
上调用
toString

通过解析
JSONArray

创建JSONArray的简单方法是:

JSONArray array = new JSONArray(Arrays.asList(StrLabels));
// The output String is simply
String StrFile = array.toString();
数据输出流 写入文件时,使用
FileOutputStream
。您可以在上面打开一个
DataOutputStream
,然后使用
writeUTF
将字符串写入文件


相反,在文件上打开一个
DataInputStream
,并调用
readUTF

实际上,如果将三个字符串放在“总计”字符串中,则很难将三个原始字符串分开,除非您遵循以下方法之一:

固定长度: 每个
字符串
的长度都是固定的:这意味着从“total”
字符串中分离出三个(甚至更多)相同长度的片段
将得到原始的三个字符串。e、 g:

String[] arrayOfStrings = {"abcde", "fghij", "klmno"}; //each string is long 5
String totalString = arrayOfString[0] + arrayOfString[1] + arrayOfString[2]; // parenthesis not needed

// now if I want to came back to the original three I have to do like this:
String[] newArrayOfStrings = new String[3]; // I create a new array that has to be filled with the original 3 strings
newArrayOfStrings[0] = totalString.substring(0,5); //cut the first part, from character number 0 incluse to character number 5 excluse (from 0 to 4)
newArrayOfStrings[1] = totalString.substring(5,10);
newArrayOfStrings[2] = totalString.substring(10,15);

System.out.println(newArrayOfString[0] + newArrayOfString[1] + newArrayOfString[2]); // this will output "abcdefghijklmno"

// you can do that process also with a cycle (if you have more than three strings together

for(int i = 0, i < arrayOfStrings.length ; i++){
    newArrayOfStrings[i] = totalString.substring((i* 5),5+(i*5));
}

分隔符: 第二种方法是使用分隔符,然后使用拆分。我将在每个字符串之间添加一个竖条“|”。
竖线出现在一行中的可能性非常小,因此不需要转义分隔符字符。如果要使用公共字符作为分隔符,必须在每个分隔符前面添加转义字符,以免混淆

String[] arrayOfStrings = {"abcde", "fghij", "klmno"}; //each string is long 5
String totalString = arrayOfString[0] + "|" + arrayOfString[1] + "|" + arrayOfString[2]; // the result is abcde|fghij|klmno

//now I split the strings from the total to a new array:
String[] newArrayOfStrings = totalString.split("|");

System.out.println(newArrayOfString[0] + newArrayOfString[1] + newArrayOfString[2]); // this will output "abcdefghijklmno"

大写字母: 另一种方法是使用大写字母分隔字符串,但这更困难,因此我将避免使用它

JSON(来自njzk2) 只需创建一个
JSONArray
,将字符串放入其中,在
JSONArray
上调用
toString
。 通过解析
JSONArray

在代码中(来自我)是:

String[]arrayOfStrings={“abcde”、“fghij”、“klmno”};
JSONArray数组=新的JSONArray();

对于(inti=0;我在“标签”之间没有分隔符)-不…更好的谷歌搜索
SharedReferences
Yes@Selvin是正确的,请看这里,但在这种情况下,您应该在用户输入的字符串中屏蔽此字符,因为用户可以轻松输入“;”字符,在这种情况下,结果是“strFile.split”我不知道我喜欢固定长度的想法。文本视图设置为最多9个字符,但如果用户只填写4,则固定长度将失败。可以在输入的文本中添加空格以确保9个空格。而(长度)您对我的答案的评论也适用于您的答案;)。
String[] arrayOfStrings = {"abcde", "fghij", "klmnopqrs"};
// first string is long 5, need to be 9
// second string long 5, need to be 9
// third string long 9, that's ok.

String totalString = ""; // initialization of the total string
for(int i = 0; i < arrayOfStrings.length; i++){
    String piece = arrayOfString[i];

    String toBeAdded = "";
    if(piece.length() < 9){
        int length = piece.length();
        toBeAdded = piece;
        while(length < 9){
            toBeAdded = " " + toBeAdded;
            length++;
        }
    } else if(piece.length() > 9){
        throw new IllegalArgumentException ("Error, string longer than 9");
    } else {
        toBeAdded = piece;
    }
    totalString = totalString + toBeAdded;
}

System.out.println(totalString); // this will be "    abcde    fghijklmnopqrs"

// to separate the strings to as the previous example but before adding in the new array you have to remove the white spaces using>
String thisIsTheOneWithoutWhiteSpaces = pieceWithSpaces.trim();
String[] arrayOfStrings = {"abcde", "fghij", "klmno"}; //each string is long 5
String totalString = arrayOfString[0] + "|" + arrayOfString[1] + "|" + arrayOfString[2]; // the result is abcde|fghij|klmno

//now I split the strings from the total to a new array:
String[] newArrayOfStrings = totalString.split("|");

System.out.println(newArrayOfString[0] + newArrayOfString[1] + newArrayOfString[2]); // this will output "abcdefghijklmno"
String[] arrayOfStrings = {"abcde", "fghij", "klmno"};
JSONArray array = new JSONArray();

for(int i=0; i<arrayOfString.length; i++){
    array.put(arrayOfStrings[i]);
}

String totalString = array.toString();

// to import the string and decode it you have to do this:

JSONArray newArray = new JSONArray(totalString);

// now each element of the array is the same of the starting array "arrayOfString"