字符串实体类的操作

package javabse;

import java.util.StringTokenizer;

public class TestString {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//测试String
long start=System.currentTimeMillis();
String str="";
for(int i=0;i<10000;i++){
str+=","+i;
}
long end=System.currentTimeMillis();
System.out.println(end-start);//结果是2182毫秒
//测试StringBuffer
long start1=System.currentTimeMillis();
StringBuffer str1=new StringBuffer();
for(int i=0;i<10000;i++){
str1.append(",").append(i);
}
long end1=System.currentTimeMillis();
System.out.println(end1-start1);//结果是10毫秒
//测试StringBuilder
long start2=System.currentTimeMillis();
StringBuilder str2=new StringBuilder();
for(int i=0;i<10000;i++){
str2.append(",").append(i);
}
long end2=System.currentTimeMillis();
System.out.println(end2-start2);//结果是10毫秒


String  s="Hello_World";
//测试String的charAt(index)方法:用于返回一个字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(s.length()-1));//最大的下标是s.length()-1
//测试提取字符串
System.out.println(s.substring(4));
System.out.println(s.substring(4,5));
//测试比较字符串
System.out.println(s.equals("Hello_World"));
System.out.println(s.equals("Hello_Worl"));
System.out.println(s.endsWith("d"));
System.out.println(s.equalsIgnoreCase("hello_world"));
System.out.println(s=="Hello_World");
System.out.println("Hello_World"=="Hello_World");
System.out.println(s.contains("w"));
System.out.println(s.concat(" china"));
System.out.println(s.compareTo("Hello_Word"));
//获取下标
System.out.println(s.indexOf('w'));//没找到
System.out.println(s.lastIndexOf('l'));//9
//匹配字符串(从正则表达式处补充)
System.out.println(s.matches("d"));//返回false
System.out.println("abcd".matches("d"));//返回false
System.out.println("abcd".matches("abcd"));//返回true
//拆分字符串
String []arr=s.split("_");
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
String b="a|b";
String []arr1=b.split("\\|");//正则表达式需要替换
for(int i=0;i<arr1.length;i++){
System.out.println(arr1[i]);
}
//替换字符串
System.out.println(s.replace('o', 'q'));
//转换大小写
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
//格式化输出,从网上找例子补充

//测试StringTokenizer分隔符
StringTokenizer st=new StringTokenizer("this is a word");//默认是空格作为分隔符
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
st=new StringTokenizer("this_is_a_word","i",true);//以i作为分割符(不算本身也算,否则)
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
st=new StringTokenizer("this_is_a_word","i");//以i作为分割符
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
String ss="this is a test";
String sa[]=ss.split("\\s");//匹配空白字符
for(int i=0;i<sa.length;i++){
System.out.println(sa[i]);
}

//StringBuffer线程安全
StringBuffer sbb=new StringBuffer("abc");
sbb.append("def");
System.out.println(sbb.insert(2,"le"));//插入
System.out.println(sbb.deleteCharAt(2));//删除
System.out.println(sbb.delete(2, 3));//删除
System.out.println(sbb.toString());
//StringBuilder线程不同步
StringBuilder sbu=new StringBuilder("sa");
sbu.append("sb");
sbu.insert(1, "12");
System.out.println(sbu.toString());
//主要区别如下:

//String是字符串常量(多用在需要拼接的字符串足够简单时)
//StringBuffer是线程安全的字符串变量(多用在多线程中)
//StringBuilder是非线程安全的字符串变量(多用在单线程中)


}

}

你可能感兴趣的:(多线程,正则表达式)