开发者

String.split("*") returns Exception

开发者 https://www.devze.com 2023-03-29 21:58 出处:网络
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 \

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

精彩评论

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

关注公众号