如何分割字符串?人在线急求
从文本框中规律性输入一行字符串,如:a,1|b,2|c,3|.....|x,n.首先要从|处分割成:a,1;b,2;c,3......x,n,然后从,处分割,然后把逗号前的字符存入到数组1中,逗号后面的字符存到数组2中,该如何解决?急求!!!
------解决方案--------------------String s= "d,d|f,d ";
String[] ss=s.split( "\\| ");
String[][] sss = new String[ss.length][];
for(int i=0;i <ss.length;i++){
sss[i]=ss[i].split( ", ");
}
------解决方案-------------------- String str = "a,1|b,2|c,3|d,4|e,5 ";
String[] arrStr = str.split( "\\| ");
String[] arrItem = null;
String[] arr1 = new String[arrStr.length];
String[] arr2 = new String[arrStr.length];
for (int i = 0; i < arrStr.length; i++) {
arrItem = arrStr[i].split( ", ");
arr1[i] = arrItem[0];
arr2[i] = arrItem[1];
}
------解决方案--------------------H:\java> javac test.java
H:\java> java test
[a, b, c, x]
[1, 2, 3, n]
H:\java>
=========================
public class test{
public static void main(String args[]){
String s = "a,1|b,2|c,3|x,n ";
String arr[] = s.split( "\\| ");
String[] a1 = new String[arr.length],a2 = new String
[arr.length];
for(int i=0;i <arr.length;i++){
String temp[] = arr[i].split( ", ");
a1[i] = temp[0];
a2[i] = temp[1];
}
System.out.println(java.util.Arrays.asList(a1));
System.out.println(java.util.Arrays.asList(a2));
}
}