String;StringBuffer;StringBuilder用法和区别

来源:互联网 发布:淘宝怎么做到包邮 编辑:程序博客网 时间:2024/06/05 18:41

1,String类,内容一旦声明则不可变,如果变,则会改变String的引用地址,即创建一个新的类,比如:

    1 String s = "1234qwe";
    2 = s+1;
    3 System.out.print(s);// result : 1234qwe1

一开始s指向1234qwe,当执行第二行代码,之前对象s并没有变化,所以我们说String类型是不可改变的对象了,由于这种机制,每当用String操作字符串时,实际上是在不断的创建新的对象,而原来的对象就会变为垃圾被GC回收掉,可想而知这样执行效率会有多低。

2,  而StringBuffer与StringBuilder就不一样了,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操作时,实际上是在一个对象上操作的,这样就不会像String一样创建一些而外的对象进行操作了,当然速度就快了。

因此,三者相比,从效率上来看StringBuilder >  StringBuffer  >  String

3,StringBuilder与 StringBuffer的区别

    StringBuilder:线程非安全的

    StringBuffer:线程安全的

    当我们在字符串缓冲区被多个线程使用是,JVM不能保证StringBuilder的操作是安全的,虽然他的速度最快,但是可以保证StringBuffer是可以正确操作的。当然大多数情况下就是我们是在单线程下进行的操作,所以大多数情况下是建议用StringBuilder而不用StringBuffer的,就是速度的原因。

如果要操作少量的数据用 = String; 单线程操作字符串缓冲区 下操作大量数据 = StringBuilder;多线程操作字符串缓冲区 下操作大量数据 = StringBuffer;


4,String 类常用的一些用法:

  1.        1, * public char[] toCharArray()将一个字符串变为字符数组  
  2.        2, * public byte[] getBytes() 将一个字符串变为字节数组  
  3.          */  
  4.         String strChar = "Hello";               //  定义一个字符串  
  5.         char cStr[] = strChar.toCharArray();    //将Hello这个字符串变为一个字符数组  
  6.         byte bStr[] = strChar.getBytes();       //将Hello这个字符串变为一个字节数组  
  7.         for(int i=0;i<cStr.length;i++){          //循环输出这个字符数组  
  8.             System.out.print(cStr[i]+"、");  
  9.         }  
  10.         System.out.println();  
  11.         for(int j=0;j<bStr.length;j++){          //循环输出这个字节数组  
  12.             System.out.print(bStr[j]+"、");  
  13.         }  
  14.           
  15.           
  16.         System.out.println();  
  17.         System.out.println("--------------------------------------------");  
  18.           
  19.         /*  
  20.         3, * public String(char[] value)将全部的字符数组变为一个字符串  
  21.         4, * public String(char[] value, int offset, int count)将指定范围内(部分)的字符数组变为字符串,offset是开始点,count是长度  
  22.         5, * public String(byte[] bytes)将全部的字节数组变为一个字符串  
  23.         6, * public String(byte[] bytes, int offset, int length)将指定范围内(部分)的字节数组变为字符串,offset是开始点,length是长度  
  24.          */  
  25.         char stringChar[] = {'I',' ','L','O','V','E',' ','Y','O','U','!'};  //定义一个字符数组  
  26.         byte b[] = {'1','2','3','4'};  
  27.         String strOne = new String (stringChar);                //将字符数组变为字符串  
  28.         String strTwo = new String (stringChar,2,8);                //将指定范围的字符数组变为字符串  
  29.         String bStrOne = new String(b);                     //将字节数组变为字符串  
  30.         String bStrTwo = new String(b,2,2);                 //将指定范围的字节数组变为字符串  
  31.         System.out.println("strOne的值是:"+strOne);  
  32.         System.out.println("strTwo的值是:"+strTwo);  
  33.         System.out.println("bStrOne的值是:"+bStrOne);  
  34.         System.out.println("bStrTwo的值是:"+bStrTwo);  
  35.         System.out.println("--------------------------------------------");     
  36.         /*  
  37.         7, * public char charAt(int index)从一个字符串中取出指定位置的字符。  
  38.          */  
  39.         String inStr = "charAt";  
  40.         char inChar = inStr.charAt(2);                  //从指定的字符串中取出指定位置的字符  
  41.         System.out.println("位置为2的地方的字符是:"+inChar);  
  42.         System.out.println("--------------------------------------------");      
  43.         /*  
  44.         8, * public int length()取得字符串的长度  
  45.          */  
  46.         String strLength = "strLength";  
  47.         int sLength = strLength.length();                   //取得字符串的长度  
  48.         System.out.println("字符串strLength的长度是:"+sLength);                  
  49.         System.out.println("--------------------------------------------");  
  50.         /*  
  51.         9, * 查找指定的字符串是否存在  
  52.          * public int indexOf(String str)从头开始查找指定字符串是否存在,返回int类型,若查找不到返回-1  
  53.          * public int indexOf(String str,int fromIndex)从指定位置开始查找指定的字符串是否存在,返回int类型,若查找不到就返回-1  
  54.          */     
  55.         String indexOfStr = "abcdefgcdhi";  
  56.         System.out.println(indexOfStr.indexOf("cd"));       //从头开始查找字符串“cd”返回2,也说明是查找到的第一个符合条件的位置。  
  57.         System.out.println(indexOfStr.indexOf("c",3));      //从第四个元素开始查找字符串“cd”  
  58.         System.out.println(indexOfStr.indexOf("x"));        //没有查找到,返回-1  
  59.         System.out.println("--------------------------------------------");   
  60.         /*  
  61.         10 * public String trim()去掉字符串中左右的空格,但中间的空格不会被去掉  
  62.          */  
  63.         String  strTrim = "   Hello Worldsd   ";  
  64.         System.out.println(strTrim.trim());         //去掉空格  
  65.         System.out.println("--------------------------------------------");  
  66.   
  67.         /*  
  68.         11, * 字符截取:从一个字符串中取出部分内容  
  69.          * substring(int beginIndex)从指定位置开始截取至字符串结尾  
  70.          * substring(int beginIndex, int endIndex)截取指定范围的字符串(开始位置和结束为止),不包括结束位置的字符  
  71.          */  
  72.         String subStringStr = "I amLucy!!!!!";  
  73.         System.out.println(subStringStr.substring(2));  
  74.         System.out.println(subStringStr.substring(2,7));        //不包括结束位置的字符  
  75.         System.out.println("--------------------------------------------");    
  76.         /*  
  77.         12, * public String[] split(String regex)拆分字符串,以某一个字符串作为拆分点  
  78.          */  
  79.         String spliteStr = "Hello Lily!";  
  80.         String spliteS[] = spliteStr.split(" ");  
  81.         for(int x=0;x<spliteS.length;x++){  
  82.             System.out.println(spliteS[x]);  
  83.         }  
  84.         System.out.println("--------------------------------------------");           
  85.         /*  
  86.         13, * public String toLowerCase()将字符串字符都变为小写  
  87.         14,* public String toUpperCase()将字符串字符都变为大写  
  88.          */            
  89.         String lowerUpper = "HelloWorld!";  
  90.         System.out.println(lowerUpper.toUpperCase());  
  91.         System.out.println(lowerUpper.toLowerCase());  
  92.         System.out.println("--------------------------------------------");     
  93.         /*  
  94.          15,* 判断是否以指定的字符串开头或者结尾  
  95.          * public boolean startsWith(String prefix)判断是否以指定的字符串开头  
  96.          * public boolean endsWith(String suffix)  判断是否以指定的字符串结尾  
  97.          */  
  98.         String str1 = "Hello,cat";  
  99.         boolean s1 = str1.startsWith("He");  
  100.         boolean s2 = str1.endsWith("cat");  
  101.         System.out.println(s1+"\t"+s2);  
  102.         System.out.println("--------------------------------------------");        
  103.         /*  
  104.         16, * 判断两个字符串是否相等  
  105.          * public boolean equals(Object anObject)   区分大小写判断两个字符串是否相等  
  106.          * public boolean equalsIgnoreCase(String anotherString) 不区分大小写判断两个字符串是否相等  
  107.          */  
  108.         String str11 = "Hello";  
  109.         String str22 = "hello";  
  110.         System.out.println("区分大小写判断两个字符串是否相等的结果是:"+str11.equals(str22));  
  111.         System.out.println("不区分大小写判断两个字符串是否相等的结果是:"+str11.equalsIgnoreCase(str22));  
  112.         System.out.println("--------------------------------------------");           
  113.         /*  
  114.          17* 字符串替换功能  
  115.          * public String replaceAll(String regex,String replacement)将指定字符regex替换成指定的字符replacement  
  116.          */  
  117.         String strReplace = "Hello";  
  118.         System.out.println("替换后的结果是:"+strReplace.replaceAll("l","x"));  
5,String buffer() 类常用的一些用法:

  1. public class UsingStringBuffer {  
  2.     /** 
  3.    1,  * 查找匹配字符串 
  4.      */  
  5.     public static void testFindStr() {  
  6.         StringBuffer sb = new StringBuffer();  
  7.         sb.append("This is a StringBuffer");  
  8.         // 返回子字符串在字符串中最先出现的位置,如果不存在,返回负数  
  9.         System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));  
  10.         // 给indexOf方法设置参数,指定匹配的起始位置  
  11.         System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"3));  
  12.         // 返回子字符串在字符串中最后出现的位置,如果不存在,返回负数  
  13.         System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));  
  14.         // 给lastIndexOf方法设置参数,指定匹配的结束位置  
  15.         System.out.println("sb.lastIndexOf(\"is\", 1) = "  
  16.                 + sb.lastIndexOf("is"1));  
  17.     }  
  18.   
  19.     /** 
  20.     2, * 截取字符串 
  21.      */  
  22.     public static void testSubStr() {  
  23.         StringBuffer sb = new StringBuffer();  
  24.         sb.append("This is a StringBuffer");  
  25.         // 默认的终止位置为字符串的末尾  
  26.         System.out.print("sb.substring(4)=" + sb.substring(4));  
  27.         // substring方法截取字符串,可以指定截取的起始位置和终止位置  
  28.         System.out.print("sb.substring(4,9)=" + sb.substring(49));  
  29.     }  
  30.   
  31.     /** 
  32.     3, * 获取字符串中某个位置的字符 
  33.      */  
  34.     public static void testCharAtStr() {  
  35.         StringBuffer sb = new StringBuffer("This is a StringBuffer");  
  36.         System.out.println(sb.charAt(sb.length() - 1));  
  37.     }  
  38.   
  39.     /** 
  40.     4, * 添加各种类型的数据到字符串的尾部 
  41.      */  
  42.     public static void testAppend() {  
  43.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  44.         sb.append(1.23f);  
  45.         System.out.println(sb.toString());  
  46.     }  
  47.   
  48.     /** 
  49.      5,* 删除字符串中的数据 
  50.      */  
  51.     public static void testDelete() {  
  52.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  53.         sb.delete(05);  
  54.         sb.deleteCharAt(sb.length() - 1);  
  55.         System.out.println(sb.toString());  
  56.     }  
  57.   
  58.     /** 
  59.     6, * 向字符串中插入各种类型的数据 
  60.      */  
  61.     public static void testInsert() {  
  62.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  63.         // 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值  
  64.         sb.insert(2'W');  
  65.         sb.insert(3new char[] { 'A''B''C' });  
  66.         sb.insert(8"abc");  
  67.         sb.insert(23);  
  68.         sb.insert(32.3f);  
  69.         sb.insert(63.75d);  
  70.         sb.insert(5, 9843L);  
  71.         sb.insert(2true);  
  72.         System.out.println("testInsert: " + sb.toString());  
  73.     }  
  74.   
  75.     /** 
  76.     7, * 替换字符串中的某些字符 
  77.      */  
  78.     public static void testReplace() {  
  79.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  80.         // 将字符串中某段字符替换成另一个字符串  
  81.         sb.replace(10, sb.length(), "Integer");  
  82.         System.out.println("testReplace: " + sb.toString());  
  83.     }  
  84.   
  85.     /** 
  86.      8,* 将字符串倒序 
  87.      */  
  88.     public static void reverseStr() {  
  89.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  90.         System.out.println(sb.reverse()); // reverse方法将字符串倒序  
  91.     }  
  92. }  
原创粉丝点击