I want to split this String A,B,C,D,"E,F",G,H
with comma(,)
operator but not split the "E,F"
.. I want this following output.
我要把这个字符串分成A,B,C,D, E,F,G,H用逗号(,)运算符,但不拆分“E,F”我希望输出如下。
A
B
C
D
E,F
G
H
5 个解决方案
#1
0
this may help:
这可以帮助:
String s = "A,B,C,D,\"E,F\",G,H";
String[] tmp = s.split(",\"|\",");
List<String> result = new ArrayList<>();
for(int i=0; i<tmp.length; i++) {
if (i % 2 == 0) {
result.addAll(Arrays.asList(tmp[i].split(",")));
}else {
result.add(tmp[i]);
}
}
The result
list contains the elements
结果列表包含元素。
#2
0
Here is an approach that does not use regexps:
这里有一个不使用regexp的方法:
private static List<String> splitQuoted(String string) {
List<String> res = new ArrayList<>();
int idx = 0;
int start = 0;
boolean inQuote = false;
while (idx < string.length()) {
char ch = string.charAt(idx++);
if (ch == '"') {
inQuote = !inQuote;
} else {
if (ch == ',' && !inQuote) {
res.add(string.substring(start, idx - 1));
start = idx;
}
}
}
if (start != idx)
res.add(string.substring(start));
return res;
}
It should scale well as the input string grows, since it only ever looks forward. You could improve upon its efficiency further by using char[]
arrays instead of String.charAt()
, too. It also leaves the quote characters in the output values, but it would be fairly trivial to remove them as you go.
它应该会随着输入字符串的增长而扩展,因为它只会期待。您可以通过使用char[]数组而不是String.charAt()来进一步提高它的效率。它还会在输出值中保留引号字符,但是在您离开时删除它们将是相当简单的。
#3
0
This simple regex will match any string ending with a comma outside "
: "([^\",]*\"[^\"]*\")*[^\",]*(,|$)"
, so you can split the string on comma or end string character matching the regex, like in this function:
这个简单的regex将匹配任何字符串以逗号结尾之外”:“([[^ ^ \”、\]*”\]* \”)*(^ \”)* | $(),这样你就可以用逗号或结束字符串的字符串来匹配正则表达式,比如在这个函数中:
private static List<String> splitByComma(String s) {
List<String> output = new ArrayList<>();
Pattern pattern = Pattern.compile("([^\",]*\"[^\"]*\")*[^\",]*(,|$)");
Matcher matcher = pattern.matcher(s);
while (matcher.find() && matcher.start() < s.length()) {
output.add(s.substring(matcher.start(), (matcher.end() == s.length())?matcher.end():matcher.end() - 1));
}
return output;
}
#4
0
regex to achieve expected results:
regex实现预期结果:
String stringToSearch = "A,B,C,D,\"E,F\",G,H";
Pattern p1 = Pattern.compile("(?:[^\",]+|\"[^\"]+\")+");
Matcher m = p1.matcher(stringToSearch);
while (m.find())
{
System.out.println(m.group());
}
#5
-3
You can fix it by using replace();
您可以使用replace()来修复它;
String replaceString=s1.replace("\"","");//replaces all occurrences of "\"" to ""
Then split .
然后分手。
#1
0
this may help:
这可以帮助:
String s = "A,B,C,D,\"E,F\",G,H";
String[] tmp = s.split(",\"|\",");
List<String> result = new ArrayList<>();
for(int i=0; i<tmp.length; i++) {
if (i % 2 == 0) {
result.addAll(Arrays.asList(tmp[i].split(",")));
}else {
result.add(tmp[i]);
}
}
The result
list contains the elements
结果列表包含元素。
#2
0
Here is an approach that does not use regexps:
这里有一个不使用regexp的方法:
private static List<String> splitQuoted(String string) {
List<String> res = new ArrayList<>();
int idx = 0;
int start = 0;
boolean inQuote = false;
while (idx < string.length()) {
char ch = string.charAt(idx++);
if (ch == '"') {
inQuote = !inQuote;
} else {
if (ch == ',' && !inQuote) {
res.add(string.substring(start, idx - 1));
start = idx;
}
}
}
if (start != idx)
res.add(string.substring(start));
return res;
}
It should scale well as the input string grows, since it only ever looks forward. You could improve upon its efficiency further by using char[]
arrays instead of String.charAt()
, too. It also leaves the quote characters in the output values, but it would be fairly trivial to remove them as you go.
它应该会随着输入字符串的增长而扩展,因为它只会期待。您可以通过使用char[]数组而不是String.charAt()来进一步提高它的效率。它还会在输出值中保留引号字符,但是在您离开时删除它们将是相当简单的。
#3
0
This simple regex will match any string ending with a comma outside "
: "([^\",]*\"[^\"]*\")*[^\",]*(,|$)"
, so you can split the string on comma or end string character matching the regex, like in this function:
这个简单的regex将匹配任何字符串以逗号结尾之外”:“([[^ ^ \”、\]*”\]* \”)*(^ \”)* | $(),这样你就可以用逗号或结束字符串的字符串来匹配正则表达式,比如在这个函数中:
private static List<String> splitByComma(String s) {
List<String> output = new ArrayList<>();
Pattern pattern = Pattern.compile("([^\",]*\"[^\"]*\")*[^\",]*(,|$)");
Matcher matcher = pattern.matcher(s);
while (matcher.find() && matcher.start() < s.length()) {
output.add(s.substring(matcher.start(), (matcher.end() == s.length())?matcher.end():matcher.end() - 1));
}
return output;
}
#4
0
regex to achieve expected results:
regex实现预期结果:
String stringToSearch = "A,B,C,D,\"E,F\",G,H";
Pattern p1 = Pattern.compile("(?:[^\",]+|\"[^\"]+\")+");
Matcher m = p1.matcher(stringToSearch);
while (m.find())
{
System.out.println(m.group());
}
#5
-3
You can fix it by using replace();
您可以使用replace()来修复它;
String replaceString=s1.replace("\"","");//replaces all occurrences of "\"" to ""
Then split .
然后分手。