开发者

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

开发者 https://www.devze.com 2023-01-18 03:00 出处:网络
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 m

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

精彩评论

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