假设我有以下字符串:
String asd = "this is test ass this is test"
我想使用“ass”字符序列分割字符串。
我用了:
asd.split("ass");
它不起作用。我需要做什么?
这对我来说似乎很好用:
public class Test
{
public static void main(String[] args) {
String asd = "this is test ass this is test";
String[] bits = asd.split("ass");
for (String bit : bits) {
System.out.println("'" + bit + "'");
}
}
}
结果:
'this is test '
' this is test'
你真正的分隔符可能不同吗?不要忘记 split 使用它的参数作为正则表达式......
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
试试这个它会工作
public class Splitter {
public static void main(final String[] args) {
final String asd = "this is test ass this is test";
final String[] parts = asd.split("ass");
for (final String part : parts) {
System.out.println(part);
}
}
}
印刷:
this is test
this is test
在 Java 6 下。您期待什么输出?