2

A very dumb question, I know " and . are both special characters, but why there is a difference when using split() function?

so here is the code

    String.split("\"");
    String.split("\\.");

They both works, but why there is only one "\" in first line of code and two in second?

Edit: What would happen if I do

    String.split("\\"");

Would the result be the same as

    String.split("\"");
Crysishy
  • 57
  • 8
  • 2
    _I know " and . are both special characters_ : No, at least not under the same context. `"` is special as in Java's String literal syntax, but nothing special in regex. `.`is special in aspect of regex, but not in Java's String literal syntax. Just print out that two strings and you should see what that means – Adrian Shum Aug 30 '17 at 03:58
  • Simply: the parameter in the first line has length 1 (the only character is "), and the parameter in the second line has length 2 (the first character is \ and the second character is a period). The regex matcher needs the \ to be in the string so that it doesn't treat the period as special. – ajb Aug 30 '17 at 03:59

3 Answers3

5

In the first example, you are escaping the " (which isn't a special regex character, so it's literally the same as string.split(Character.toString('"'));) - that is, you need to escape the " to put " in a String - you could also write string.split("" + '"');. In the second example, you are escaping the . (which is a special regex character). Thus the \ is escaped, and is equivalent to Pattern.compile(".", Pattern.LITERAL).split(string);

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

When you use split function in Java you should also take care that some symbols means more than just simple character, it might also means special command for regex.

For string    For regex
   "\""           "      <- noting special from regex point of view, will process all (")
   "."            .      <- Yeh, special character, I will process all characters
   "\\."          \.     <- noting special from regex point of view, will process all (.)
Yuriy Gyerts
  • 1,464
  • 18
  • 30
1

Already answered here The split() method in Java does not work on a dot (.)

java.lang.String.split splits on regular expressions, and . in a regular expression means "any character".

Yuriy Gyerts
  • 1,464
  • 18
  • 30