StringBuilder,StringBuffer和String,以及常用的方法

Stringbuilder与String的区别:
String程序运行时会额外创建一个对象,保存String的字符串。当频繁操作字符串时,就会额外产生很多临时变量。StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。至于 StringBuilder 和StringBuffer ,它们基本相似,不同之处,StringBuffer 是线程安全的,而 StringBuilder 则没有实现线程安全功能(不能同步访问),所以性能略高。因此一般情况下,如果需要创建一个内容可变的字符串对象,应优先考虑使用 StringBuilder 类。


StringBuilder中的方法:
 
    // 创建一个空的StringBuilder对象
        StringBuilder str=new StringBuilder();
        // 追加字符串
        str.append("jaewkjldfxmopzdm");
        
        // 从后往前每隔三位插入逗号
        for(int i=str.length()-3;i>0;i=i-3){
        str.insert(i,",");
        }
 // 将StringBuilder对象转换为String对象并输出
        System.out.print(str.toString());
    }
与String相似的方法
序号 方法描述
1 int capacity() 返回当前容量。
2 char charAt(int index) 返回此序列中
3 void ensureCapacity(int minimumCapacity) 确保容量至少等于指定的最小值。
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此序列复制到目标字符数组 dst。
5 int indexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引。
6 int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
7 int lastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引。
8 int lastIndexOf(String str, int fromIndex) 返回 String 对象中子字符串最后出现的位置。
9 int length()  返回长度(字符数)。
测试代码以及注释
 
import java.util.Arrays;
public class StringBuilderTest
{
    public static void main(String[] args)
    {
        StringBuilder str=new StringBuilder("fxhellobl");
       // int capacity();返回当前容量
        int num=str.capacity();
        System.out.println("capacity方法返回当前容量为:"+num);
        //ensureCapacity方法更改容器最小值为30
        str.ensureCapacity(30);
        int num2=str.capacity();
        System.out.println("更改后的容量为:"+num2);
        //追加数组用append方法
        str.append("qwewqqwergf");
        int num3=str.capacity();
        System.out.println("追加后的容量为:"+num3);
        //使用charAt(int index)方法返回一个值
        System.out.println("返回在数组第6的字母为:"+str.charAt(5));
        //void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此序列复制到目标字符数组 dst。
        char []a=new char[10];
        str.getChars(2,7,a,0);
        System.out.println(a);
        //通过toString方法将字符数组a转化成a1字符串
        String a1=Arrays.toString(a);
        //通过charAt方法返回index的字符
        System.out.println(a1);
        System.out.println(a1.charAt(1));
        //通过indexOf方法返回字符串出现的位置
        int n=a1.indexOf("o");
        System.out.println("o出现的位置是第"+n+"个");
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
        //空格和逗号都属于字符要计算在内
        //int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
        int n2=a1.indexOf("l",4);
        System.out.println("l从第4索引开始第一次出现的索引:"+n2);
        //int lastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引。
        int n3=a1.lastIndexOf("l");
        System.out.println("l最后出现的索引:"+n3);
        //返回长度
        int n4=a1.length();
        System.out.println("a1长度为:"+n4);
    }
}
在测试以上方法时发现StringBuilder的容量和字符串不符
特定了解了一下(不一定正确)
StringBuilder的容量变化规则:
首先初始容量为16;    
当新建一个StringBuilder时创建了数据时
容量为16+数据的长度
其中minimumCapacity即为(count + len)的值,即 (已使用的字符个数 + 追加的新串的长度)的和(16 + 3 = 19),暂且叫最小容量值。
当存储的字符所需的空间大于这个数的时候,StringBuilder 会自动增大内存,增加Capacity 
容量呈16的倍数增加
一般来说容量capacity会为数据预留出16个字节的空间

猜你喜欢

转载自blog.csdn.net/weixin_41657883/article/details/80845228