I have the following string I need to split by double quotes. below is the sample string
JavaScript
x
String raw = ""AKINWALE JOHNSON ROTIMI " "100000072 " "1985"
+ "0217 " " " "0250000066 ""
+ "" 08035558619" "22324902758 " "NG0250002 " "9113985 "";
When I try to split the string by double quotes, knowing that some quotes can be empty
I tried the following : String[] split = raw.split(""(\w\s+|\s+)"");
it is close but I seem to be missing something.
Advertisement
Answer
You can simply split it on \s*"\s*
which means "
preceded by or followed by zero or more whitespace character(s).
Demo:
JavaScript
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String raw = ""AKINWALE JOHNSON ROTIMI " "100000072 " "1985"
+ "0217 " " " "0250000066 ""
+ "" 08035558619" "22324902758 " "NG0250002 " "9113985 "";
String[] split = raw.split("\s*"\s*");
Arrays.stream(split).forEach(System.out::println);
}
}
Output:
JavaScript
AKINWALE JOHNSON ROTIMI
100000072
19850217
0250000066
08035558619
22324902758
NG0250002
9113985