How can I split the following word in to an array
That\'s the code
into
array
0 That
1 s
2 the
3 code
I tried
You can split by a regex that would be one of the two characters - quote or space:
String[] strs = str.split("['\\s]");
You can use OR
in regular expression
public static void main(String[] args) {
String str = "That's the code";
String[] strs = str.split("'|\\s");
for (String sstr : strs) {
System.out.println(sstr);
}
}
The string will be split by single quote (') or space. The single quote doesn't need to be escaped. The output would be
run:
That
s
the
code
BUILD SUCCESSFUL (total time: 0 seconds)