java中StringBuffer的用法


JAVA中StringBuffer类常用方法详解





转载

2012年05月03日 14:48:33

        <ul class="article_tags clearfix csdn-tracking-statistics" data-mod="popu_377">
            <li class="tit">标签:</li>


  • java /
  • string /
  • integer /
  • class /
  • c /




  • String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串方面的效率比String高了很多。

     

    在java中有3个类来负责字符的操作。

      1.Character 是进行单个字符操作的,

      2.String 对一串字符进行操作。不可变类。

      3.StringBuffer 也是对一串字符进行操作,但是可变类。

    1. public class UsingStringBuffer {  
    2.     /** 
    3.      * 查找匹配字符串 
    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.      * 截取字符串 
    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.      * 获取字符串中某个位置的字符 
    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.      * 添加各种类型的数据到字符串的尾部 
    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.      * 删除字符串中的数据 
    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.      * 向字符串中插入各种类型的数据 
    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.      * 替换字符串中的某些字符 
    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.      * 将字符串倒序 
    87.      */  
    88.     public static void reverseStr() {  
    89.         StringBuffer sb = new StringBuffer(“This is a StringBuffer!”);  
    90.         System.out.println(sb.reverse()); // reverse方法将字符串倒序  
    91.     }  
    92. }  
    public class UsingStringBuffer {
        /**
         * 查找匹配字符串
         */
        public static void testFindStr() {
            StringBuffer sb = new StringBuffer();
            sb.append("This is a StringBuffer");
            // 返回子字符串在字符串中最先出现的位置,如果不存在,返回负数
            System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));
            // 给indexOf方法设置参数,指定匹配的起始位置
            System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is", 3));
            // 返回子字符串在字符串中最后出现的位置,如果不存在,返回负数
            System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));
            // 给lastIndexOf方法设置参数,指定匹配的结束位置
            System.out.println("sb.lastIndexOf(\"is\", 1) = "
                    + sb.lastIndexOf("is", 1));
        }
    
        /**
         * 截取字符串
         */
        public static void testSubStr() {
            StringBuffer sb = new StringBuffer();
            sb.append("This is a StringBuffer");
            // 默认的终止位置为字符串的末尾
            System.out.print("sb.substring(4)=" + sb.substring(4));
            // substring方法截取字符串,可以指定截取的起始位置和终止位置
            System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));
        }
    
        /**
         * 获取字符串中某个位置的字符
         */
        public static void testCharAtStr() {
            StringBuffer sb = new StringBuffer("This is a StringBuffer");
            System.out.println(sb.charAt(sb.length() - 1));
        }
    
        /**
         * 添加各种类型的数据到字符串的尾部
         */
        public static void testAppend() {
            StringBuffer sb = new StringBuffer("This is a StringBuffer!");
            sb.append(1.23f);
            System.out.println(sb.toString());
        }
    
        /**
         * 删除字符串中的数据
         */
        public static void testDelete() {
            StringBuffer sb = new StringBuffer("This is a StringBuffer!");
            sb.delete(0, 5);
            sb.deleteCharAt(sb.length() - 1);
            System.out.println(sb.toString());
        }
    
        /**
         * 向字符串中插入各种类型的数据
         */
        public static void testInsert() {
            StringBuffer sb = new StringBuffer("This is a StringBuffer!");
            // 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值
            sb.insert(2, 'W');
            sb.insert(3, new char[] { 'A', 'B', 'C' });
            sb.insert(8, "abc");
            sb.insert(2, 3);
            sb.insert(3, 2.3f);
            sb.insert(6, 3.75d);
            sb.insert(5, 9843L);
            sb.insert(2, true);
            System.out.println("testInsert: " + sb.toString());
        }
    
        /**
         * 替换字符串中的某些字符
         */
        public static void testReplace() {
            StringBuffer sb = new StringBuffer("This is a StringBuffer!");
            // 将字符串中某段字符替换成另一个字符串
            sb.replace(10, sb.length(), "Integer");
            System.out.println("testReplace: " + sb.toString());
        }
    
        /**
         * 将字符串倒序
         */
        public static void reverseStr() {
            StringBuffer sb = new StringBuffer("This is a StringBuffer!");
            System.out.println(sb.reverse()); // reverse方法将字符串倒序
        }
    }
    小结:
    StringBuffer不是不变类,在修改字符串内容时,不会创建新的对象,因此,它比String类更适合修改字符串。
    StringBuffer类没有提供同String一样的toCharArray方法
    StringBuffer类的replace方法同String类的replace方法不同,它的replace方法有三个参数,第一个参数指定被替换子串的起始位置,第二个参数指定被替换子串的终止位置,第三个参数指定新子串

    </article>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值