Java 拆分一个字符串(以前的代码),其中我要分隔int';s和string';s |字符串包含分隔符
例如,我正在尝试拆分字符串Java 拆分一个字符串(以前的代码),其中我要分隔int';s和string';s |字符串包含分隔符,java,string,Java,String,例如,我正在尝试拆分字符串 String line = "(0, 10, 20, 'string value, 1, 2, 2', 100, 'another string', 'string, string, text', 0)"; 我希望将其拆分,因此我将使用“0”、“10”、“20”、“字符串值、1、2、2”等,而不是“0”、“10”、“20”、“字符串值”、“1”、“2”、“2”。如果我正确理解了您的问题(尽量更具体一些:))您希望拆分字符串以实现以下输出: "0","10","20"
String line = "(0, 10, 20, 'string value, 1, 2, 2', 100, 'another string', 'string, string, text', 0)";
我希望将其拆分,因此我将使用“0”、“10”、“20”、“字符串值、1、2、2”等,而不是“0”、“10”、“20”、“字符串值”、“1”、“2”、“2”。如果我正确理解了您的问题(尽量更具体一些:))您希望拆分字符串以实现以下输出:
"0","10","20","string value, 1, 2, 2","100","another string","string, string, text","0"
我很想试一试,现在是:
String line = "(0, 10, 20, 'string value, 1, 2, 2', 100, 'another string', 'string, string, text', 0)";
char splitString[] = line.toCharArray();
List<String> foundStrings = new ArrayList<String>();
for (int x = 0; x < splitString.length;x++){
String found = "";
if (Character.isDigit(splitString[x])) {
while(Character.isDigit(splitString[x])) {
found += Character.toString(splitString[x]);
x++;
}
foundStrings.add(found);
x --;
}
if (x < splitString.length) {
int count = 0;
int indexOfNext = 0;
if (splitString[x] == '\'') {
int startIndex = x + 1;
count = startIndex;
char currentChar = 0;
char c = '\'';
while(currentChar != c) {
currentChar = splitString[count];
count ++;
currentChar = splitString[count];
}
indexOfNext = count;
for (int j = startIndex; j < indexOfNext; j++){
found += Character.toString(splitString[j]);
}
foundStrings.add(found.trim());
x = indexOfNext;
}
}
}
for (int p = 0; p < foundStrings.size();p++) {
if (p > 0) System.out.print(",");
System.out.print("\"" + foundStrings.get(p) + "\"");
}
String line=“(0,10,20,'字符串值,1,2,2',100,'另一个字符串','字符串,字符串,文本',0)”;
char splitString[]=line.toCharArray();
List foundStrings=new ArrayList();
对于(int x=0;x0)系统输出打印(“,”);
System.out.print(“\”+foundStrings.get(p)+“\”);
}
其他人可能有更优雅的解决方案。祝你好运 你试过什么。有什么问题吗?你能说得更具体些吗?关于这个有什么不清楚的?他希望在不在单引号内的逗号处拆分,并删除单引号。因此,单引号将
字符串值1、2、2
转换为一个单位,不能在逗号处进行分割;而0、10、20等之间的逗号确实会导致拆分。真遗憾,我不能给出答案。