String str = a.split(".")[0];
This will throw ArrayOutOfBoundException
Why is this?
This is because "." is a reserved character in regular expression, representing any character.
Instead, we should use the following statement:
String str = a.split("\\.")[0];
When the code is compiled, the regular expression is known as "\.", which is what we want it to be
25 comments:
hi i am splitting an email address
first i split at @
then the second part i split at "." to get domain but gives error
code:
result = s[j].split("@");
uname[j] = result[0];
res = result[1].split("\\.");
domain[j] = res[res.length-1];
uname is stored properly
problem in third line
gives error 1 at cmd prompt
String[] result="hjx@umich.edu".split("@");
String uname = result[0];
String[] res = result[1].split("\\.");
String domain = res[res.length-1];
System.out.println(uname + " " + domain);
I run it without any error on my PC using JDK 1.6.
Can you post your error messages here?
thanks for the practical info. may the force with you!
Thanks for that! The split(".") was driving me nuts ;-)
Thx
thanks
Thanks very help full
thank you veryyyyyyyyyyyyyy much!
here is another good link on split String in Java which compares both split and StringTokenizer.
very nice!
Great, thanks a lot
Great help, Thanks :D
Thanks. You're awesome. Important thing like this is not mentioned in Javadoc.
Hi, I have a follow up question
in
String str = a.split("\\.")[0];
what is the use of [0]?
thanks!
even it comes late, there are two steps in one.
a.split("\\.")[0]
==
String[] aSplitted = a.split("\\.");
aSplitted[0]
split returns an array, so you can handle it as an array.
Good to know!
Thanks for sharing
great! Saved a lot of my time.
thx bro
Oh yeah you saved me so much.
Thankyou for you information.
Thankyou verymuch
Great article ..I need some more information about Java Swing..Can anyone help me out?
best dissertation services
DOT is a reserved simbol on regular expressions. So, it must be escaped as "\."
BUT \ is ALSO a reserved simbol. Must be escaped too: "\\."
AT LAST, \ is a reserved simbol in java strings. The correct solution must be:
a.split("\\\\.")
:-)
Thanks a lot. I spent many time to resolve it!
Nice Article... visit more java examples
Great and Nice article... Thanks for sharing your information and Views..
Java Training in Chennai
Post a Comment