开发者

JAVA REGEX - Split a string with "?" as delimiter

I want to split a string with "?" as the delimiter.

str.split("?")[0]开发者_开发百科 fails.


The argument to the "split" method must be a regular expression, and the '?' character has special meaning in regular expressions so you have to escape it. That's done by adding a backslash before it in the regexp. However, since the regexp is being supplied by way of a Java String, it requires two backslashes instead so as to get an actual backslash character into the regexp:

str.split( "\\?" )[0];


str.split("\\?")[0]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜