String.split ( "." ) not working?

2,654 views
Skip to first unread message

mona

unread,
Oct 5, 2006, 11:47:48 PM10/5/06
to Google Web Toolkit
Hi:

I'm usin GWT 1.1.10. When I tried to split a string on the dot symbol
using String.split ( "." ), I get an array of the length of the entire
string with nothing in each of the array element. I then tried
String.split ( "\.") but then the GWT compiler says "Invalid escape
sequence". I think this is a bug.

Basically, I would like to split a string like "a.b.c" and get "a",
"b", and "c" separately.

thanks,
Mona

Ian Bambury

unread,
Oct 5, 2006, 11:58:52 PM10/5/06
to Google-We...@googlegroups.com
No. Not a bug. You have to escape the escape character! :o)
 
backslash-dot is invalid because Java doesn't need to escape the dot.
 
You've got to escape the escape character to get it as far as the regex which is used to split the string. eg.

String[] x = ((String)"123.234.345").split("\\.");
Window.alert(x[1]);
 
will display "234"
 
Ian

Ian Bambury

unread,
Oct 6, 2006, 12:22:12 AM10/6/06
to Google-We...@googlegroups.com
Alternatively you can use split("[.]")

mona

unread,
Oct 6, 2006, 6:26:08 PM10/6/06
to Google Web Toolkit
I tried the String.split ( "[.]" ) and that worked, thanks Ian!

cheers,
Mona

Reply all
Reply to author
Forward
0 new messages