如何把一个逗号分隔的字符串转换成数组 并且倒序输出

package Interview;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;


/*
 * 如何把一个逗号分隔的字符串转换成数组   并且倒序输出
 * 
 */
public class SplitByComma {

String[] result ;
//最简单的解决方案
public SplitByComma(String orgStr){

result = orgStr.split(",", -1);
for(int i = result.length - 1 ; i >= 0 ; i--) //倒序输出
System.out.printf(result[i]+"\t");
}

public static void main(String [] args){
System.out.println("解决方案一:");
SplitByComma str = new SplitByComma("hello,java,welcome,to,my,world");
System.out.println("\n解决方案二:");
str.tokenizer("hello,java,welcome,to,my,world");
System.out.println("解决方案三:");
str.indexof("hello,java,welcome,to,my,world");
}
//解决方法二
public String[] tokenizer(String s){
StringTokenizer tokener = new StringTokenizer(s,",");
String[] result = new String[tokener.countTokens()];
Integer i = 0 ;
while(tokener.hasMoreTokens()){
result[i++] = tokener.nextToken();
}
System.out.println(Arrays.toString(result));
return result ;
}
//解决方案三
public String[] indexof(String s){

int index = -1 ;
int oldIndex = 0 ;
List ss = new ArrayList();
while((index = s.indexOf(',' , index+1)) != -1){
ss.add(s.substring(oldIndex, index));
oldIndex = index + 1;
}
if(s.charAt(s.length() - 1) == ','){
ss.add("");
}
String[] array = ss.toArray(new String[ss.size()]);
System.out.println(Arrays.toString(array));
return array;

}


}

你可能感兴趣的:(如何把一个逗号分隔的字符串转换成数组 并且倒序输出)