0

我设置了 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 个字符串,但我还需要反转该过程并读取文件。有没有办法将每个 StrLabels [] 与 StrFile 分开。

谢谢 Gianmarco,这是在您的帮助下的最终代码。我反转了 toBeAdded,所以它添加到字符串的末尾而不是前面。

      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;
4

4 回答 4

1

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

JSON

只需创建一个JSONArray,将您的字符串放入其中,调用toString.JSONArray

通过在 a 中解析读取的字符串来反转JSONArray

创建 JSONArray 的简单方法是:

JSONArray array = new JSONArray(Arrays.asList(StrLabels));
// The output String is simply
String StrFile = array.toString();

数据输出流

写入文件时,您使用FileOutputStream. 您可以DataOutputStream在上面打开一个,并使用writeUTF将您的字符串写入文件。

DataInputStream通过在文件上打开 a 并调用来反转readUTF

于 2013-09-19T14:32:35.447 回答
0

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

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

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

String[] StrLabels = StrFile.split("\n");

于 2013-09-19T14:27:34.167 回答
0

You can put some special character you chose between those string, or put new line characters before saving them like this( i chose ";" for ex. )

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

and then once you read it from your file, you can use:

    strFile.split(";");

Which returns you an array of Strings.

于 2013-09-19T14:24:40.320 回答
0

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

固定长度:

每个String的长度都是固定的:这意味着将三个(甚至更多)相同长度的片段从“总”中分离出来,String您将拥有原始的三个字符串。例如:

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的 s 是 9 个字符长并且必须是 9 个字符长。如果它们更短,我们将在String. 对于这个例子,我将使用前一个例子的数组,稍作改动。

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"


大写字母:

另一种方法是使用大写字母来分割字符串,但这比较困难,所以我会避免它。

JSON(来自 njzk2)

只需创建一个JSONArray,将您的字符串放入其中,调用toString. JSONArray通过在 a 中解析读取的字符串来反转JSONArray

在代码中(来自我)是:

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"
于 2013-09-19T14:38:45.217 回答