我想将字符串拆分成单个单词。如果字符串为:
String s = "Practice makes perfect. you'll only get Perfect by practice. just practice!";
我想存储为:
String[] str = {"Practice", "makes", "perfect", "youll", "only", "get", "Perfect", "by", "practice", "just", "practice"};
如果使用
s.split("\\W")
,则you'll
会变成you ll
。如何将其设置为youll
? 最佳答案
可以先删除':
String[] str = s.replace("'", "").split("\\W+");
还将
+
添加到拆分中,以避免阵列中的空插槽。输出:
[Practice, makes, perfect, youll, only, get, Perfect, by, practice, just, practice]