gpt4 book ai didi

java - 按单词拆分java中的字符串

转载 作者:搜寻专家 更新时间:2023-10-31 08:06:02 25 4
gpt4 key购买 nike

如何将下面的单词拆分成一个数组

这就是代码

进入

array
0 That
1 s
2 the
3 code

我试过这样的东西

String str = "That's the code";

String[] strs = str.split("\\'");
for (String sstr : strs) {
System.out.println(sstr);
}

但是输出是

That
s the code

最佳答案

专门针对空格和撇号进行拆分:

public class Split {
public static void main(String[] args) {
String [] tokens = "That's the code".split("[\\s']");
for(String s:tokens){
System.out.println(s);
}
}
}

或拆分任何非单词字符:

public class Split {
public static void main(String[] args) {
String [] tokens = "That's the code".split("[\\W]");
for(String s:tokens){
System.out.println(s);
}
}
}

关于java - 按单词拆分java中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com