20

In my Java application I need to find indices and split strings using the same "target" for both occasions. The target is simply a dot.

Finding indices (by indexOf and lastIndexOf) does not use regex, so

String target = ".";
String someString = "123.456";
int index = someString.indexOf(target); // index == 3

gives me the index I need.

However, I also want to use this "target" to split some strings. But now the target string is interpreted as a regex string. So I can't use the same target string as before when I want to split a string...

String target = ".";
String someString = "123.456";
String[] someStringSplit = someString.split(target); // someStringSplit is an empty array

So I need either of the following:

  1. A way to split into an array by a non-regex target
  2. A way to "convert" a non-regex target string into a regex string

Can someone help? Would you agree that it seems a bit odd of the standard java platform to use regex for "split" while not using regex for "indexOf"?

birgersp
  • 2,907
  • 4
  • 25
  • 53
  • 1
    Why can't you simply escape the decimal in the split regex? – OneCricketeer Aug 19 '16 at 11:39
  • You could use the substring method with your indexOf, and extract the string before and the string after the token. That is, if you really want to avoid using `split`. – AntonH Aug 19 '16 at 11:45
  • @cricket_007: If I understand you correctly, you're suggesting I use `target = "\\."`. Then I can't do `someString.indexOf(target);` – birgersp Aug 19 '16 at 11:47
  • @AntonH: I'm afraid I need a more general solution than that... – birgersp Aug 19 '16 at 11:47
  • @Birger You would have to give more precisions if you want a more general solution. Is the `indexOf` going to be one character, or multiple? Or with special characters? Are you sure there's only going to be one seperation character, or possibly several? What is it that you're trying to do that isn't basically re-inventing regexes? – AntonH Aug 19 '16 at 11:51

3 Answers3

31

You need to escape your "target" in order to use it as a regex. Try

String[] someStringSplit = someString.split(Pattern.quote(target));

and let me know if that helps.

Tim Hallyburton
  • 2,217
  • 1
  • 17
  • 29
2

You can try this one.

String target = ".";
String someString = "123.456";
StringTokenizer tokenValue = new StringTokenizer(someString, target);

while (tokenValue.hasMoreTokens()) {
    System.out.println(tokenValue.nextToken());
}
Sejal Rudani
  • 172
  • 1
  • 17
2

String::split do split without regex if the regex is:

  • a one-char String and this character is not one of the RegEx's meta characters .$|()[{^?*+\\
  • two-char String and the first char is the backslash and the second is not the ascii digit or ascii letter.

Please see String::split() source code for details.

For escaped '.' target it is going to be split without regex.

Mike Shauneu
  • 2,755
  • 14
  • 19
  • You didn't really read the question. I was asking how to use the same string in both `indexOf` and `split`. – birgersp Oct 07 '20 at 11:42