假设我有以下字符串:
String asd = "this is test ass this is test"
我想用“屁股”字符序列拆分字符串。
我用过:
asd.split("ass");
它不起作用。我需要做什么?
答案 0 :(得分:9)
这似乎对我很好:
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将其参数用作正则表达式...
答案 1 :(得分:1)
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
试试这个会起作用
答案 2 :(得分:0)
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.你期望什么输出?