java中的string用法

来源:百度知道 编辑:UC知道 时间:2024/07/06 00:04:21
定义一个字符串s
String s = "a,1,5,6,2,3,b,d,f,w,c,e,^,&,@,8,";
将s中的字符以“,”分隔,再将数字和字母以从前到后的顺序输出
如输出数字是:1,2,3……
输出字母是:a,b,c,……

看看这个

public class T1 {

public static void main(String[] agrs) {
String s = "a,1,5,6,2,3,b,d,f,w,c,e,^,&,@,8,";
String [] array=s.split(",");
String intArray ="" ;
String strArray = "";
String other = "";

for(int i=0;i<array.length;i++){
if ('0' <= array[i].charAt(0) && array[i].charAt(0) <= '9')
intArray = intArray + array[i].charAt(0) + ",";
else if (('a' <= array[i].charAt(0) && array[i].charAt(0) <= 'z')
|| ('A' <= array[i].charAt(0) && array[i].charAt(0) <= 'Z'))
strArray = strArray + array[i].charAt(0) + ",";
else{
other = other + array[i].charAt(0)+",";
}
}
System.out.println(intArray);
System.out.println(strArray);
System.out.println(other);
}

}