实际上,如果您将三个字符串简单地放在“总”字符串中,除非您遵循以下方法之一,否则很难将三个原始字符串分开:
固定长度:
每个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"