原帖http://topic.csdn.net/u/20100109/17/aedf464b-6ad3-437e-bf40-d9db8073b6db.html
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//System.out.println(u2l("axmanTestString"));
String str="iAmAxmanIAmAGreatManWhoAreYou";
String s = null;
Long start=System.currentTimeMillis();
for(int i=0;i <1000000;i++){
s = u2l1(str);
}
Long end=System.currentTimeMillis();
System.out.println(end-start);//输出代码运行时间
System.out.println(s);
Long start1=System.currentTimeMillis();
for(int i=0;i <1000000;i++){
s = u2l(str);
}
Long end1=System.currentTimeMillis();
System.out.println(end1-start1);//输出代码运行时间
System.out.println(s);
}
public static String u2l1(String str){
for(int j=0;j <str.length();j++)
{
if((int)str.charAt(j) <=90&&(int)str.charAt(j)>=65&&j!=0)
str=str.substring(0,j)+"_"+str.substring(j,j+1).toLowerCase()+str.substring(j+1,str.length());
}
return str;
}
public static String u2l(String str){
char[] buf = new char[str.length()*2];
char[] src = str.toCharArray();
int pos = 0;
for(int i=0;i <src.length;i++){
if(65 <= src[i] && src[i] <= 90){
buf[pos++] = '_';
buf[pos++] = (char)(src[i] + ('a' - 'A'));//或直接 + 32
}else
buf[pos++] = src[i];
}
return new String(buf,0,pos);
}
}