开发者

String.split("*") returns Exception

String.split("*") return Excepti开发者_如何学JAVAon in Android Eclipse

Is there Any solution...


String#split("*") should throw an exception. String#split accepts a regular expression string, and "*" is an invalid regular expression. The * means "zero or more of the previous item" but there is no previous item.

If you're trying to split literally on asterisks, use: split("\\*"). There are two backslashes because you need to pass a backslash to the regular expression parser to tell it that the * is literal, and of course this is in a string, so to get a backslash, you have to escape it. Hence, two.

String[] splitOnAsterisk = "one*two*three".split("\\*");


completely guessing here, but have you tried escaping the *? i.e String.split("\*")? Also what are you trying to split on?


Do this instead

String[] s1 = s.split("\\*");


You want:

String.split("[*]")

the split function takes a regex expression

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜