2011-10-02 102 views
2

例如Java - 將字符串拆分爲新字符串?

String s = "You should always brush your teeth before every meal in the day."; 

但我希望做的是分裂部成新句子,像這樣:

String s = "You should always brush your teeth/nbefore every meal in the day."; 

所以結果是這樣的:

String s = "You should always brush your teeth"; 

String s2 = "before every meal in the day."; 

基本上,我希望它搜索「/n」,然後將下一句與現在分開。

+1

分裂的第二個字符串是容易的。但是如果你想分割第一個字符串,這被稱爲* word wrapping *,這是一個完全不同的主題。 –

回答

2
String[] str = s.split("/n"); 
if(str.length == 2) { 
    s = str[0]; 
    s2 = str[1]; 
} 
0

您可以將字符串分割成字符串數組做

String[] chunks = s.split("\\n"); 
相關問題