2010-04-05 46 views

回答

11
"hello hallo tjena hej tere".split(" "); 

請注意,split方法的參數是一個正則表達式。請參閱方法here的文檔。

+0

快速,準確,精確的鏈接到文檔。這是一個勝利者。 – 2010-04-05 18:53:40

+0

不幸的是,該鏈接已經死亡。 – 2011-09-16 14:39:36

0

String.split應該是你所需要的。

String myString = "hello hallo tjena hej tere"; 
String[] words = myString.split(" "); 
1

commons-lang有一個共同的合同更好地執行。

String[] splits = StringUtils.split("hello hallo tjena hej tere"); 
+0

我認爲String.split()是足夠的,不需要包含任何其他的API。 – helpermethod 2010-04-05 19:30:19

+0

公平地說,當commons-lang最初在2002年發佈時,這將非常有價值。當時,很多人因爲各種原因而被困在Java 1.3中,並且String.split(和一般的正則表達式功能)還沒有可用。 – Syntactic 2010-04-05 21:05:57

+0

String.split在處理字符串末尾的匹配時有一些意想不到的*特性。看看Google的Splitter課程。 – whiskeysierra 2010-04-05 21:55:12