[英]How to split String and put it on ArrayList?
here is my String . 这是我的弦乐 。 [NOT A Arraylist]
[不是数组列表]
[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]
How to convert it to Arraylist as belows 如何将其转换为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 ", "
so that split item 02
incorrectly. 以便错误地拆分
item 02
。
This should do it 这应该做
String s = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";
Then using the Arrays.asList() along with a split on the seperator ", "
然后使用Arrays.asList()具有沿着分割的分隔符
", "
List<String> list = new ArrayList<String>(Arrays.asList(s.split(", ")));
To your comment, are you sure? 对您的评论,确定吗?
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)));
Returns 退货
true
Here's a much simpler solution using Regular expressions. 这是使用正则表达式的简单得多的解决方案。
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() + "*");
}
}
}
Output of this is 这个的输出是
*[1]*
*[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]*
*[22630]*
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);
}
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 !
I have complied it is working. 我认为它正在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.