字符串分割
String string="Victor-Tang - Qing";
String[] temp;
temp=string.split("-");
for(String string2:temp) {
System.out.println(string2);
}
字符串分隔
String string="Victor-Tang - Qing";
StringTokenizer str=new StringTokenizer(string);
StringTokenizer(string,"-");
while (str.hasMoreElements()) {
System.out.println(str.nextElement());
}
字符串反转
String string="Victor-Tang - Qing";
String resove=new StringBuffer(string).reverse().toString();
System.out.println(resove);
字符串大小写转换
String string3="Ictortang";
System.out.println("原始字符串:"+string3);
String string4 = string3.toUpperCase();
System.out.println("转换之后的字符串" + string4);
String string5 = string3.toLowerCase();
System.out.println("转换之后的字符串"+string5);
判断字符串是否相等
String str1="tangqing";
String str2="tangqing";
if(str1.equals(str2)) {
System.out.println("字符串相等");
} else {
System.out.println("字符串不相等");
}
字符串性能测试
long startTime=System.currentTimeMillis();
System.out.println(startTime);
for(int i=0;i<50000;i++) {
String strs1="hello";
String strs2="hello";
}
long endTime=System.currentTimeMillis();
System.out.println("第一种方法总共耗时"+(endTime-startTime));
long starTime1=System.currentTimeMillis();
for(int j=0;j<50000;j++) {
String str3=new String("hello");
String str4=new String("hello");
}
long endTime1=System.currentTimeMillis();
System.out.println("第二种方法总共耗时"+(endTime1-starTime1));
String s1=new String("tangqing");
System.out.println(s1.intern());
字符串格式化
double e=Math.E;
System.out.println(e);
System.out.format("%.2f", e);
System.out.println(String.format("%.2f", e));
DecimalFormat decimalFormat=new DecimalFormat(".00");
System.out.println(decimalFormat.format(e));
字符串拼接
String str3="victor";
StringBuffer stringBuffer=new StringBuffer(str3);
System.out.println(stringBuffer);
System.out.println(stringBuffer.append("victor"));
字符串查找
String str1="My Name";
String str2="is Victor";
System.out.println(str1+" "+str2);
String str3="Na";
System.out.println(str1.indexOf(str3));
String str4="i";
System.out.println(str2.lastIndexOf(str4));
System.out.println(str2.charAt(4));
System.out.println(str2.startsWith("is"));
System.out.println(str1.endsWith("S"));
字符串截取
String str = "hello Java,hello PHP";
System.out.println(str.substring(1));
字符串替换
String string="ttor angor";
String rString=string.replace("or","mm");
System.out.println(rString);
String words = "hello java,hello php";
String newStr = words.replaceFirst("hello","你好 ");
System.out.println(newStr);
String words = "hello java,hello php";
String newStr = words.replaceAll("hello","你好 ");
System.out.println(newStr);