如何拆分String并将其放在ArrayList上?

时间:2013-11-13 05:33:41

标签: java regex string arraylist

这是我的字符串。 [不是Arraylist]

[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]

如何将其转换为Arraylist,如下所示

item 01 : [1]
item 02 : [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]
item 03 :  [22630]

EDITED

[1], [<2254... here also matched ", "
...0720,C,D>,<2254,890... here also matched ", "

以便错误地拆分item 02

4 个答案:

答案 0 :(得分:3)

这应该这样做

String s = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";

然后在分隔符上使用Arrays.asList()split ", "

List<String> list = new ArrayList<String>(Arrays.asList(s.split(", ")));

对你的评论,你确定吗?

String itemTwo = "[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]";
System.out.println(itemTwo.equals(list.get(1)));

返回

true

答案 1 :(得分:2)

这是使用正则表达式的更简单的解决方案。

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestRegularExpression2 {

    private static String input = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";

    public static void main(String[] args) {

        // Matches everything between the braces                
        Pattern pattern = Pattern.compile("\\[(.*?)\\]");
        Matcher matcher = pattern.matcher(input);   

        // Prints out each of the matching strings. 
        while (matcher.find()) {
            System.out.println("*" + matcher.group() + "*");
        }

    }

}

输出

*[1]*
*[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]*
*[22630]*

答案 2 :(得分:1)

String str = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";

List<String> aList = new ArrayList<String>();

for (String s1 : str.split(", ")) {
    aList.add(s1);
}

答案 3 :(得分:0)

import java.util.ArrayList;
import java.util.Arrays;


/**
 * @author JFVARUGH
 * here is my String. [NOT A Arraylist] [1],
 *  [1],
 *  [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], ...
 *
 *item 01 : [1]
  item 02 : [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]
  item 03 : [22630]
 */
public class StringProblel {

    @SuppressWarnings("null")
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int index =0, tempIndex=0,stringIndex=0;
        char[] keyWrds =  {'[',']',','};
        char[][] temp = new char[1000][1000];

        String sourceStr = "[1],[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630] ";

        while(index < sourceStr.length()){
            if( sourceStr.charAt(index) == keyWrds[0] ){
                temp[stringIndex][tempIndex] = sourceStr.charAt(index);
                while(sourceStr.charAt(index) != keyWrds[1] && index < sourceStr.length()){
                    index++;
                    temp[stringIndex][++tempIndex] = sourceStr.charAt(index);                   
                }

                stringIndex++;              
            }
            index++;
        }
        System.out.println(temp[1]);
    }

}


The time complexity will be O(n2). There is scope for improvements !

我已经遵守它正在运作。