简体   繁体   中英

Splitting string gives null result in java

i am trying very simple splitting. I dont know why it is not working.

String abc= "192.168.120.2";
String[] eachByteabc= abc.split("."); 

When I debug it and see, I get the result that abc contains : 192.168.120.2. But when I do split, it does not give me error but gives me null result. I think, i have made some silly mistake. Can you tell me where I am wrong. What should I do. Thank you in advance.

You need to escape the . , since it's a regex operator. Change it to:

String[] eachByteabc= abc.split("[.]"); 

Addition, thanks to @sparks :

While this will work, the [] characters in regex are used to annotate a set, so if you are looking for where it might be in a limited series of characters, you should use them.

In this case - use \\\\. to escape the . character.

试试吧=):

String[] eachByteabc= abc.split("\\.");

public String[] split(String regex) takes a regular expression as an argument. You must escape the point, since it's a regex operator.

String[] eachByteabc = abc.split("."); is not eorr,but you can not to debug and Watch the values.use String[] eachByteabc = abc.split(".");you can Watch values in the debug.

String.split uses regex, so you need to use abc.split("\\\\.");

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM