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]
精彩评论