2008-10-15 66 views
-1

假設我有以下字符串:如何在固定字符序列上分割字符串?

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

,我想分割使用「屁股」的字符序列的字符串。

我用:

asd.split("ass"); 

它不工作。我需要做什麼?

+0

我會用'「這是測試FOO這是測試」`作爲一個例子雖然:) – 2009-12-06 22:40:52

回答

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是你期待什麼輸出?

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' 

是您真正的分隔符不同吧?不要忘記,分裂使用其參數爲正則表達式...

1
String asd = "this is test foo this is test"; 
String[] parts = asd.split("foo"); 

試試這個它會工作