Java – how to split without regular expressions
In my java application, I need to use the same "target" to find the index and split the string in both cases The goal is just a point
The lookup index (through indexof and lastIndexOf) does not use regular expressions, so
String target = "."; String someString = "123.456"; int index = someString.indexOf(target); // index == 3
Gave me the index I needed
However, I also want to use this "target" to split some strings But now the target string is interpreted as a regular expression string So when I want to split a string, I can't use the same target string as before
String target = "."; String someString = "123.456"; String[] someStringSplit = someString.split(target); // someStringSplit is an empty array
So I need any of the following:
>How to split a non regular expression target into arrays > how to "convert" a non regular expression target string to a regular expression string
Can I help you? Do you agree that it seems strange that the standard Java platform uses regular expressions for "splitting" instead of the regular expression "indexof"?
Solution
You need to avoid "target" to use it as a regular expression
String[] someStringSplit = someString.split(Pattern.quote(target));
If it helps, please let me know