-3

我已经拆分了一个字符串,但是如何将每个拆分部分放入自己的字符串中?示例:其中一个拆分部分会说:220532我如何将它放入自己的字符串中以便我以后可以使用它?我一直在寻找很多东西,但我没有找到任何可以帮助我的东西,也许我找错地方了,我不知道。

我的一段代码(拆分代码):

public void actionPerformed(ActionEvent e) {
    String delim = (", ");
    StringTokenizer st = new StringTokenizer("225433,1690,29040474 267067,77,1529611     
    237690,78,1788405 273749,79,1854885 274972,79,1952641 268946,71,867189 239419,64,420791 
    238742,81,2319417 159198,85,3530900 138028,90,5475735 208678,78,1696957 174967,79,1869053 
    290115,65,469553 212042,65,486662 359576,48,84972 201637,70,790755 240043,55,167763 
    282705,52,132888 244545,56,198147 261757,56,201357 281466,39,34650 239398,57,214448 
    100420,76,1347559 250011,51,115880 144608,72,912505 166813,67,577751 -1,1,-1 -1,-1 -1,-1 
    -1,-1 -1,-1 -1,-1 73106,748 -1,-1 -1,-1 92834,659 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 
    -1,-1", delim);

    while (st.hasMoreTokens()) {
        System.out.println(st.nextToken());
     }

}
}); 
4

4 回答 4

2

假设你有一个像这样的字符串

String number="1,2,3,4,5";
String num[]=number.split(","); //This statement split the number using delimiter ","
               //means num[0]="1",num[1]="2" and so on..
于 2013-08-21T17:12:11.630 回答
1

You can store each piece in a List (part of the Java Collections framework)...

public void actionPerformed(ActionEvent e) {
    String delim = ", ";
    StringTokenizer st = new StringTokenizer("225433,1690,29040474 267067,77,1529611 237690,78,1788405 273749,79,1854885 274972,79,1952641 268946,71,867189 239419,64,420791 238742,81,2319417 159198,85,3530900 138028,90,5475735 208678,78,1696957 174967,79,1869053 290115,65,469553 212042,65,486662 359576,48,84972 201637,70,790755 240043,55,167763 282705,52,132888 244545,56,198147 261757,56,201357 281466,39,34650 239398,57,214448 100420,76,1347559 250011,51,115880 144608,72,912505 166813,67,577751 -1,1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 73106,748 -1,-1 -1,-1 92834,659 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1",
                    delim);
    List<String> parts = new ArrayList<String>();
    while (st.hasMoreTokens()) {
        parts.add(st.nextToken());
    }
    for (String s : parts) {
        // Do something with s
    }
}

Or you can use something like String.split:

public void actionPerformed(ActionEvent e) {
    String delim = ", ";
    String input = "225433, ....";
    String[] parts = input.split(delim);
    for (String s : parts) {
        // Do something with s
    }
}
于 2013-08-21T17:10:30.260 回答
1

创建一个字符串列表并将每个标记添加到其中。

ArrayList<String> myStrings = new ArrayList<String>();
while(st.hasMoreTokens())
{
   myStrings.add(st.nextToken());
}

现在myStrings包含您从较长的字符串中拆分出来的所有单个字符串。

编辑:然后要访问它们,您可以使用 for-each 循环(或标准 for 循环)。

for(String number : myStrings)
{
    System.out.println(number);
    //sample output: "225433"
    //               "1690"
}

或者

for(i = 0; i < myStrings.size(); i++)
{
    System.out.println("Number " + i + " is: " + myStrings.get(i));
    //example output: "Number 0 is: 225433"
    //                "Number 1 is: 1690"
}

前者更简洁,但如果你想知道你正在使用的元素的索引,那么后者是必要的。

如果你只想要第一个元素,那就是myStrings.get(0)

于 2013-08-21T17:10:56.417 回答
1

用这个:

String[] splittedText = new String("your text").split(",\\s+|\\s+|,");

如果有空格或昏迷和空格,它将分裂。它适用于您的号码。

例子:

String[] splittedText = new String("225433,1690,29040474 267067,77,1529611     237690,78,1788405 273749,79,1854885 274972").split(",\\s+|\\s+|,");
        for(String s : splittedText)
            System.out.println(s);
于 2013-08-21T18:08:16.517 回答