0

文字列配列から各単語を取得するのに苦労しています。ここはコードの一部です:

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

次に、各単語を wordS[] にストックし、wordS[] にストックされた各単語を文字列に変換した後、

例 :

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 を入力したかどうかを検出します。正常に動作しますが、次のように入力すると関数の例を更新したいと思います: ^word4 ^secondword6 関数は最後の単語のみを返しますが、ユーザーが入力したすべての単語が必要です。

4

1 に答える 1

1

文字列配列の使い方を修正

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

次に、インデックスを使用して各文字列を取得します。例: words[i]iはインデックスです。Stringしたがって、 for each を個別に宣言する代わりに、追加変数を宣言せず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);
于 2013-08-30T19:07:14.220 に答える