Java 拆分方法不能正常工作?

Java split method is not working properly?

这是我的一段代码,我想用 $ 符号拆分字符串,但字符串没有被吐出。

这是我的代码:

   String str="first$third$nine%seventh";
   String s[]=str.split("$");
   System.out.println(s[0]);

输出是整个字符串:

first$third$nine%seventh

split takes a regular expression 作为参数。 $ 是正则表达式中的魔法字符。

如果用反斜杠转义它,它将被用作普通字符而不是特殊的正则表达式字符。

String s[]=str.split("\$");

这在字符串class中是很常见的事情。 Ans 这已经被问到,答案可以在 Whosebug 中找到。

split方法中的正则表达式符号应该转义。 $,?,*,^,+ 这样的字符太多了,在 split 方法中作为参数使用时应该转义。