0

In java split method when we split the sentence by "." . Why we write "\." instead of "." ?? But when we split the sentence by comma we use only ",".

  • 5
    because it's a regex you pass, not a flat String. in a regex '.' is a special character – Stultuske Jun 21 '20 at 16:59
  • 1
    In addition to above comment, you can find the list of special characters in [docs](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) – Sercan Jun 21 '20 at 17:02
  • For all special characters we use " \\ "? – The Gentelmen 24 Jun 21 '20 at 17:03
  • In regular expressions `,` matches `,` . `\\.` matches `.` . `.` matches any character because `.` is a special regex character meaning "any character". See [What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – khelwood Jun 21 '20 at 17:04
  • 1
    To keep it readable: `string.split(Pattern.quote("."))`. – Andy Turner Jun 21 '20 at 17:07

0 Answers0