簡體   English   中英

字符串數組轉換為具有格式的字符串

[英]String array to string with format

我設置了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;

一個基本的解決方案是使用分隔符,但這將意味着在分隔符位於字符串中的情況下進行處理,這意味着要確保該分隔符工作很麻煩。 一個更簡單的解決方案是依靠現有方法。

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反向操作

您可以在這些字符串之間放置一個選擇的特殊字符,或者在保存這樣的字符之前放置換行符(我選擇了“;”作為示例。)

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

然后從文件中讀取后,即可使用:

    strFile.split(";");

這將返回一個字符串數組。

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

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

當您從文件中讀取它時,請執行以下操作:

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

實際上,如果您將三個字符串簡單地放入“總計”字符串中,則除非您遵循以下方法之一,否則很難將三個原始字符串相除:

固定長度:

每個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的長度為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();


定界符:

第二種方法是使用定界符,然后使用拆分。 我將添加一個豎線“ |” 每個字符串之間。
一行中的豎線出現的可能性很小,因此您無需轉義分隔符char。 如果要使用公共字符作為分隔符,則必須在每個分隔符的前面添加一個轉義字符,不要混淆。

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 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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM