(一)字符串->数组
1、
public int getCount(String str,String sign){//查找某一字符串中str,特定子串s的出现次数
if(str==null) return 0;
StringTokenizer s=new StringTokenizer(str,sign);
return s.countTokens();
}
public String[] getArray(String str,String sign){//按特定子串s为标记,将子串截成数组。
int count=getCount(str,sign);
int j=0;
String[] arr=new String[count];
for(int i=0;i<count;i++){
if(str.indexOf(sign)!=-1){
j =str.indexOf(sign);
arr[i]=str.substring(0,j);
str =str.substring(j+1);
}else{
arr[i]=str;
}
}
return arr;
}
2、
public static String[] split(String source){
if(source==null||source.trim().equals(""))
return null;
StringTokenizer commaToker = new StringTokenizer(source,",");
String[] result = new String[commaToker.countTokens()];
int i=0;
while(commaToker.hasMoreTokens()){
result[i] = commaToker.nextToken();
i++;
}
return result;
}
(二)数组->字符串
var arr = ["a","b","c"];
arr.join(",")//输出a,b,c 相当于arr.toString();
arr.join("");//输出abc
arr.join();//等于用逗号将他们分割arr.join(",");也相当于arr.toString();输出a,b,c