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
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
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
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:
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;
}
Here is an approach that does not use regexps:
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.
regex to achieve expected results:
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());
}
You can fix it by using replace();
String replaceString=s1.replace("\"","");//replaces all occurrences of "\"" to ""
Then split .