日期:2014-05-20 浏览次数:20793 次
import java.util.Arrays; /** * Created by IntelliJ IDEA. * User: Administrator * Date: 2011-9-30 * Time: 13:25:53 * To change this template use File | Settings | File Templates. */ public class StringHandler { public static String[] splitYourStr(String str){ String[] yourWant=new String[4]; String connect="_"; int a=str.indexOf(connect); int b=str.indexOf(connect,a+1); int c=str.indexOf(connect,b+1); yourWant[0]=str.substring(0,a); yourWant[1]=str.substring(a+1,b); yourWant[2]=str.substring(b+1,c); yourWant[3]=str.substring(c+1); System.out.println(Arrays.toString(yourWant)); return yourWant; } public static void main(String[] args){ String str="AND_ThalassemiaGeneTest_TGTAlphaBehind__3.7α"; // String str="AND_ThalassemiaGeneTest_TGTAlphaBehind__ _SEA"; String[] r=splitYourStr(str); System.out.println(Arrays.toString(r)); } }
------解决方案--------------------
楼主你看这样怎么样
String str="AND_ThalassemiaGeneTest_TGTAlphaBehind_BJFKD";
String s1=str.substring(0,str.lastIndexOf("_"));
String s2=str.substring(str.lastIndexOf("_")+1,str.length());
String[] arr=s1.split("_");
arr=Arrays.copyOf(arr,arr.length+1);
arr[arr.length]=s2;
for(String s:arr){
System.out.println(s);
}
------解决方案--------------------
还是写一个吧,不管对你是否有用
static String[] getResult(String src){ String[] result = new String[4]; int pre = 0; for(int i=0; i<3; i++){ int index = src.indexOf("_",pre); result[i] = src.substring(pre,index); pre = index+1; } result[3] = src.substring(pre); return result; }