0

I am looking to parse a string by ". ", but it seems to register the period as a backspace and then just parse by the space. Why is this happening and what can I do to fix it?

String x = "Hi. My name is Jeffrey. I like sports.";
    for (String t : x.split(". "))
        System.out.println(t);

This yields:

M

nam

i

Jeffrey

(blank line)

lik

sports.

1 Answers1

1

This is because String.split():

Splits this string around matches of the given regular expression.

In regex, . matches any character. To match the literal period, escape the expression:

x.split("\\. ")
GBlodgett
  • 12,704
  • 4
  • 31
  • 45