0

我有一个形式的字符串:

str = "word1_word2_word3_word4_word5"

我想在"_"分隔符的帮助下在一个单独的变量中检索每个关键字。

例子:str = "hello_how_are_you_?"

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

感谢您的帮助,并为我的学校英语感到抱歉。

4

5 回答 5

3

尝试:

String str = "hello_how_are_you_?";
String item[]=str.split("_")   
于 2012-08-24T10:13:31.103 回答
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]; // ?
于 2012-08-24T10:13:50.743 回答
2

只需使用 split 即可。

 /* 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]);
于 2012-08-24T10:14:47.253 回答
1
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

于 2012-08-24T10:16:19.767 回答
1
    String str="hello_how_are_you_?";
    String[] strSplit=str.split("_");
于 2012-08-24T10:16:42.170 回答