2

I'm sure I'm just overlooking something here...

Is there a simple way to split a String on an explicit character without applying RegEx rules?

For instance, I receive a string with a dynamic delimiter, I know the 5th character defines the delimiter.

String s = "This,is,a,sample";

For this, it's simple to do

String delimiter = String.valueOf(s.charAt(4));
String[] result = s.split(delimiter);

However, when I have a delimiter that's a special RegEx character, this doesn't work:

String s = "This*is*a*sample";

So... is there a way to split the string on an explicit character without trying to apply extra RegEx rules? I feel like I must be missing something pretty simple.

Trebla
  • 1,027
  • 1
  • 11
  • 28

5 Answers5

9

split uses a regular expression as its argument. * is a meta-character used to match zero of more characters in regular expressions, You could use Pattern#quote to avoid interpreting the character

String[] result = s.split(Pattern.quote(delimiter));
Reimeus
  • 152,723
  • 12
  • 195
  • 261
  • Perfect, I knew there was something simple I was missing, just didn't know the right search term. Thanks! – Trebla Dec 20 '13 at 14:44
  • A note for people finding this answer in google: be aware that the most common implementation of `split` explicitly checks for backslash+character delimiters that do not have special meaning in a regular expression, and runs a fast local loop instead of using the full `Pattern` engine. By surrounding your character with `\Q...\E` (what `quote()` is doing), the delimiter will no longer trigger the fast path but instead create a new Pattern object on every call. So, benchmark your code to see if this speed makes a difference, and if it does consider *carefully* escaping the character instead. – Ti Strga May 04 '20 at 15:57
1

You need not to worry about the character type If you use Pattern

Pattern regex = Pattern.compile(s.charAt(4));
Matcher matcher = regex.matcher(yourString);
if (matcher.find()){
    //do something
}
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
1

You can run Pattern.quote on the delimiter before feeding it in. This will create a string literal and escape any regex specific chars:

delimiter = Pattern.quote(delimiter);
Kevin DiTraglia
  • 24,092
  • 16
  • 88
  • 134
0

StringUtils.split(s, delimiter);

That will treat the delimiter as just a character, not use it like a regex.

StringUtils is a part of the ApacheCommons library, which is tons of useful methods. It is worth taking a look, could save you some time in the future.

forgivenson
  • 4,234
  • 2
  • 16
  • 28
0

Simply put your delimiter between []

String delimiter = "["+s.charAt(4)+"]";
String[] result = s.split(delimiter);

Since [ ] is the regex matches any characters between [ ]. You can also specify a list of delimiters like [*,.+-]