zoukankan      html  css  js  c++  java
  • Java StringBuffer类

    StringBuffer概述

    线程安全的可变字符序列。

    与String的区别

    String是一个不可变的字符序列。
    StringBuffer是一个可变的字符序列。

    StringBuffer类的构造方法

    public StringBuffer() 无参构造方法
    public StringBuffer(int capacity) 指定容量的字符串缓冲对象
    public StringBuffer(String str) 指定字符串内容的字符串缓冲区对象

    StringBuffer类的方法

    public int capacity() 返回当前容量 是理论值
    public int length() 返回长度(字符数) 是实际值

    public class test {
        public static void main(String[] args) {
            StringBuffer sb1=new StringBuffer();
            System.out.println(sb1.capacity()); //容器的初始容量,实际值
            System.out.println(sb1.length());    //容器中字符个数,理论值
            
            StringBuffer sb2=new StringBuffer(10);
            System.out.println(sb2.capacity());
            System.out.println(sb2.length());
            
            StringBuffer sb3=new StringBuffer("Lily");
            System.out.println(sb3.capacity());    //字符串的length+初始容量
            System.out.println(sb3.length());    //实际字符个数
        }
    }
    输出:
    16
    0
    10
    0
    20
    4
    

    StringBuffer的添加功能

    public StringBuffer append(String str) 可以把任意类型的数据添加到字符串缓冲区里面,并返回字符串缓冲区本身。
    public StringBuffer Insert(int offset,String str) 在指定的位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身。
    StringBuffer是字符串缓冲区,当new的时候是在堆内存创建了一个对象,底层是一个长度为16的字符数组,当调用添加的方法时,不会再重新创建对象,而是在不断的向原缓冲区添加字符。

    public class test {
        public static void main(String[] args) {
            StringBuffer sb1=new StringBuffer();
            StringBuffer sb2=sb1.append(true);
            StringBuffer sb3=sb2.append("Lily");
            StringBuffer sb4=sb3.append(100);
            
            System.out.println(sb1.toString());
            System.out.println(sb2.toString());
            System.out.println(sb3.toString());
            System.out.println(sb4.toString());
            
        }
    }
    输出:
    trueLily100
    trueLily100
    trueLily100
    trueLily100
    
    public class test {
        public static void main(String[] args) {
            StringBuffer sb=new StringBuffer("1234567");
            sb.insert(4,"Lily");  //在指定的位置添加元素,如果没有指定位置的索引就会报索引越界
            System.out.println(sb);
            
        }
    }
    输出:
    1234Lily567
    

    StringBuffer的删除功能

    public StringBuffer deleteCharAt(int index) 删除指定位置的字符,并返回本身
    public StringBuffer delete(int start,int end) 删除从指定位置开始指定位置结束的内容,并返回本身

    public class test {
        public static void main(String[] args) {
            StringBuffer sb=new StringBuffer();
            sb.append("Lily0608");
            sb.deleteCharAt(5);  //根据索引删除索引位置上对应的字符
            System.out.println(sb);
            sb.delete(4,7);    //删除的时候包含头,不包含尾
            System.out.println(sb);
    
        }
    }
    输出
    Lily008
    Lily
    

    StringBuffer的替换和反转功能

    public StringBuffer replace(int start,int end,String str) 从start开始到end结束用str替换
    public StringBuffer reverse() 字符串反转

    public class test {
        public static void main(String[] args) {
            StringBuffer sb=new StringBuffer("Lily0608");
            sb.replace(4, 8, "0419");   //替换
            System.out.println(sb);
            
            sb.reverse();      //反转
            System.out.println(sb);
        }
    }
    输出:
    Lily0419
    9140yliL
    

    StringBuffer的截取功能

    public String substring(int start) 从指定位置截取到末尾
    public String substring(int start,int end) 截取从指定位置开始到结束位置,包含开始位置,不包含结束位置
    注意:返回值类型不再是StringBuffer本身

    StringBuffer 和String的相互转换

    String转换为StringBuffer

    1.使用构造方法
    2.通过append()方法

    StringBuffer转换为String

    1.通过构造方法
    2.通过toString()方法
    3.通过subString(0,length)

    public class test {
        public static void main(String[] args) {
            StringBuffer sb=new StringBuffer("Lily0608");  //使用构造方法把String转换为StringBuffer
            System.out.println(sb);
            
            StringBuffer sb1=new StringBuffer();  //通过append()方法把String转换为StringBuffer
            sb1.append("Lily0419");
            System.out.println(sb1);
        }
    }
    输出:
    Lily0608
    Lily0419
    
    public class test {
        public static void main(String[] args) {
            StringBuffer sb=new StringBuffer("Lily0608");  
            
            String s1=new String(sb);  //使用构造方法把StringBuffer转换为String
            System.out.println(s1);
            
            String s2=sb.toString();   //通过toString()方法把StringBuffer转换为String
            System.out.println(s2);
            
            String s3=sb.substring(0, 4);  //通过subString(0,length)把StringBuffer转换为String
            System.out.println(s3);
        }
    }
    输出:
    Lily0608
    Lily0608
    Lily
    

    把数组转化为字符串

    public class test {
        public static void main(String[] args) {
            int[] arr={10,20,30,40,50,60};
            String s=arrayToString(arr);
            System.out.println(s);
        }
        
        public static String arrayToString(int[] arr){
            StringBuffer sb=new StringBuffer(); //创建字符串缓冲区对象
            sb.append("[");      //将[添加到缓冲区
            for(int i=0;i<arr.length;i++){   //遍历数组
                if(i==arr.length-1)
                    sb.append(arr[i]).append("]");
                else
                    sb.append(arr[i]).append(", ");
            }
            return sb.toString();
            
        }
    }
    输出:
    [10, 20, 30, 40, 50, 60]
    

    字符串反转

    public class test {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入一个字符串:");
            String s=sc.nextLine();
            
            StringBuffer sb=new StringBuffer(s);
            sb.reverse();
            System.out.println("反转后:");
            System.out.println(sb);
        }
        
    }
    输出:
    请输入一个字符串:
    my name is Lily
    反转后:
    yliL si eman ym
    

    String、StringBuffer和StringBuilder的区别

    String是一个不可变的字符序列
    StringBuffer和StringBuilder是可变的字符序列
    StringBuffer是jdk1.0版本的,是线程安全的,效率低
    StringBuilder是jdk1.5版本的,是线程不安全的,效率高

    String和StringBuffer分别作为参数传递

    String类虽然是引用数据类型,但是它当作参数传递时和基本数据类型是一样的

    public class test {
        public static void main(String[] args) {
            String s="Lily";
            System.out.println("改变前:");
            System.out.println(s);
            change(s);
            System.out.println("String作为参数传递,改变后:");
            System.out.println(s);
                    
            StringBuffer sb=new StringBuffer();
            sb.append(s);
            change(sb);
            System.out.println("StringBuffer作为参数传递,改变后:");
            System.out.println(sb);
            
        }
        public static void change(String str){
            str.replace('y','i');
        }
        public static void change(StringBuffer sb){
            sb.append("0608");
        }
    }
    输出:
    改变前:
    Lily
    String作为参数传递,改变后:
    Lily
    StringBuffer作为参数传递,改变后:
    Lily0608
    
  • 相关阅读:
    自定义checkbox样式
    自定义select样式
    jsonp
    I/O复用 poll简介
    DOS和DDOS攻击
    TCP状态转换图解析
    Makefile入门
    I/O复用select 使用简介
    替换文本内容
    share memory
  • 原文地址:https://www.cnblogs.com/bbn0111/p/7495496.html
Copyright © 2011-2022 走看看