次の形式の文字列があります。
str = "word1_word2_word3_word4_word5"
"_"
howセパレーターを使用して、各キーワードを個別の変数で取得したいと考えています。
例:str = "hello_how_are_you_?"
for str1=hello, str2=how, str3=are, str4=you and str4=?
助けてくれてありがとう、そして私の学校の英語でごめんなさい。
試す:
String str = "hello_how_are_you_?";
String item[]=str.split("_")
String str = "hello_how_are_you_?"
String[] words = str.split("_");
出力:
words[0]; // hello
words[1]; // how
words[2]; // are
words[3]; // you
words[4]; // ?
それには分割を使用してください。
/* 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]);
String phrase = "hello_how_are_you_?";
String[] tokens = phrase.split("_");
for( String str: tokens)
System.out.println(str);
このコードは、これを画面に出力します。
hello
how
are
you
?
とにかく、何か Paraclete を作成しようとしている場合は、そのような文字列にデータを保存するより良い方法があるはずです。
より複雑なものが必要な場合のチュートリアルを次に示します: http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html
String str="hello_how_are_you_?";
String[] strSplit=str.split("_");