2012-08-24 35 views
0

的元素我有以下形式的字符串:如何恢復一個String

str = "word1_word2_word3_word4_word5" 

,我想找回在一個單獨的變量每個關鍵字與"_"幫助如何分離。

例如:str = "hello_how_are_you_?"

for str1=hello, str2=how, str3=are, str4=you and str4=? 

感謝您的幫助,對不起我的中學英語。

回答

3
String str = "hello_how_are_you_?" 
String[] words = str.split("_"); 

輸出:

words[0]; // hello 
words[1]; // how 
words[2]; // are 
words[3]; // you 
words[4]; // ? 
3

嘗試:

String str = "hello_how_are_you_?"; 
String item[]=str.split("_") 
2

只要使用分裂它。

/* String to split. */ 
    String str = "one-two-three"; 
    String[] temp; 

    /* delimiter */ 
    String delimiter = "-"; 
    /* given string will be split by the argument delimiter provided. */ 
    temp = str.split(delimiter); 
    /* print substrings */ 
    for(int i =0; i < temp.length ; i++) 
    System.out.println(temp[i]); 
1
String phrase = "hello_how_are_you_?"; 
String[] tokens = phrase.split("_"); 

for(String str: tokens) 
    System.out.println(str); 

這段代碼將打印此屏幕上:反正

hello 
how 
are 
you 
? 

,如果你想使一些保惠師應該有一個更好的辦法做到那麼它在保存數據這樣的字符串。

這裏是萬一教程,您需要更復雜的東西,然後由一個字符分割:http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html

1
String str="hello_how_are_you_?"; 
    String[] strSplit=str.split("_");