Java's split() method

lozina

Lifer
Sep 10, 2001
11,711
8
81
How do you split on a period (".") ?

I have the String "1.2.3" and want to split on the period.

Doing: String s[] = "1.2.3".split(".")

does not work, returns empty array. I assume it's because the period is part of regular expression syntax (I think it indicates all?). Is there an escape character in regex so I can use the period?
 

talyn00

Golden Member
Oct 18, 2003
1,666
0
0
try putting a backslash to force the . to be recognized as a regular character?
String s[] = "1.2.3".split("\.")
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Ok just figured it out. Had to use the following string: "\\x2e". which translate into regular expression \x2e and x indicates hex and 2e is hex for the period character
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Originally posted by: talyn00
try putting a backslash to force the . to be recognized as a regular character?
String s[] = "1.2.3".split("\.")

oh shoot, that works too :) thanks, that's much nicer