-1

假设我有以下字符串:

String asd = "this is test ass this is test"

我想使用“ass”字符序列分割字符串。

我用了:

asd.split("ass");

它不起作用。我需要做什么?

4

3 回答 3

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 使用它的参数作为正则表达式......

于 2008-10-15T14:47:10.000 回答
1
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");

试试这个它会工作

于 2016-07-27T09:42:36.043 回答
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 下。您期待什么输出?

于 2008-10-15T14:51:27.417 回答