2013-08-30 38 views
0

我堅持從字符串數組 在這裏得到每個字是部分代碼:獲取每個字

String wordS[] = mystring.split(" "); 

那麼我想獲得在放養每個字字[],並經過轉換在字放養每個字[]轉換成String

例如:

mystring = " hi everyone how are you " 

String[] wordS = mystring.split(" "); 

String word1 = word1 from wordS[](Hi) 

String word2 = word2 from wordS[](eeryone) 

String word3 = word3 from ... for max 20 strings wich contain 1 word 

編輯:

這裏是我的代碼:

String txt = tv.getText().toString(); 

       Pattern mat = Pattern.compile("(?<=\\^)([^0-9]+[0-9]+)|([^^0-9]+$)"); 
       Matcher m = mat.matcher(txt); 
       while (m.find()) 
       { 
        String match = m.group(); 
        String n= match.replaceFirst("^[^0-9]+", ""); 
        if (n.length() >= 1) 
        { 

         int i = Integer.parseInt(n); 

         switch (i) 
         { 
          case 0: 
           s = "<font color ='black'>"; 
           break; 
          case 1: 
           s = "<font color ='red'>"; 
           break; 
          case 2: 
           String green = "#7CFC00"; 
           s = "<font color ='" + green + "'>"; 
           break; 
          case 3: 
           String gold ="#FFD700"; 
           s = "<font color ='" + gold + "' >"; 
           break; 
          case 4: 
           s = "<font color ='blue' >"; 
           break; 
          case 5: 
           String cyan ="#00FFFF"; 
           s = "<font color ='" + cyan + "' >"; 
           break; 
          case 6: 
           String magenta ="#FF00FF"; 
           s = "<font color ='" + magenta + "' >"; 
           break; 
          case 7: 
           String peru ="#FFC0CB"; 
           s = "<font color ='" + peru + "' >"; 
           break; 
          case 8: 
           String gray ="#808080"; 
           s = "<font color ='" + gray + "' >"; 
           break; 
          case 9: 
           String orange ="#FFA500"; 
           s = "<font color ='" + orange + "' >"; 
           break; 

          default: 
           s = "<font color ='white'>"; 
         } 

         String replace = match.replaceFirst("[0-9]+$", ""); 
         String[] wordS = replace.split(" "); 
         showf.setText(Html.fromHtml(s + "" + replace + "</font>")); 


        } 
        else 
        { 
         showf.setText(match.replaceFirst("[0-9]+$", "")); 
        } 
       }  

此功能上色字符串的一部分,如果用戶鍵入的它將檢測^ TEST2 < - 「2」 = INT其分配顏色。如果Y型:^ blablabla5它將返回青色文字顏色。工作正常,但現在我想更新我的功能的例子,如果我輸入:^ word4^secondword6 我的功能將只返回最後一個單詞,但我希望由用戶輸入的所有單詞 有你想法嗎?

+0

與數組中的元素一樣創建儘可能多的變量有什麼意義?事實上,你不能那樣做。您無法在運行時創建變量。 –

回答

1

正確的字符串數組的使用

String [] words = mystring.split(" "); 

然後使用索引來獲得每個字符串。例如:單詞[i] i是索引。 因此,不用爲每個單獨聲明String,而只需使用words[0],words[1]等而不聲明加法變量。它作爲執行以下操作相同:

String s1 = words[0]; 
String s2 = words[1]; 
... 

至於表現在TextView,它不知道你要在TextView顯示什麼。

TextView text = (TextView) findViewById(Your_Textviw_ID); // you might have declared it in an XML somewhere. 
text.setText(the_sring_you_want_to_set_it_to); 
+0

你讀過這個問題了嗎? –

+0

如何使用索引@Shobhit Puri – Clyx

+0

@RohitJain對不起,你感覺如此。我只是認爲提問者的意圖是能夠分開陳述並使用單個字符串。所以我相應地回答了。如果你有不同的感覺,你能不能把你的輸入加入它?謝謝。 –