String,StringBuilder与StringBuffer

String

Java中字符串属于对象,Java提供了String类来创建和操作字符串
String的赋值方法可以为:String str = “…” 或 String str = New String("…")。
其中String创建的字符串存储在公共池中,而new创建的字符串对象存储在堆上。
String类对象创建完毕后无法进行内容更改
用于获取有关对象的信息的方法称为访问器方法。
String 类的一个访问器方法是 length() 方法,它返回字符串对象包含的字符数。
String类提供了两个连接字符串的方法
1.string1.concat(string2) 该方法的返回值为string1与string2连接之后生成的新字符串
2.使用"+"来连接字符串
若不想生成新的字符串则可使用StringBuffer或StringBuilder

StringBuffer与StringBuilder

当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象。
StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。

StringBuilder类的常用方法

1.append方法

public StringBuilder append(String str) {//传入一个String对象
super.append(str);//调用父类方法
return this;//返回当前对象。
}

方法描述:将指定的字符串追加到此字符序列。

该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:

public AbstractStringBuilder append(String str) {//传入一个String对象str

    if (str == null)//判断当前对象是否为空
        return appendNull();//为空返回 该类中appendNull()方法生成的对象,该对象中有一个数组value[3]中存储了null四个字符。
    int len = str.length();//len变量中存储当前字符串对象的长度
    ensureCapacityInternal(count + len);//调用该类中的private void ensureCapacityInternal(int minimumCapacity)方法传入count + len。
    //其中count是原字符串对象中的字符串长度,len为将要拼接的字符串的长度。判断原数组长度是否足够存储拼接后的字符串,不够   则扩容

    str.getChars(0, len, value, count);//调用String类中的public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)方法,将str数组从0到len复制到value中,偏移量为count(从第count个开始复制)
    count += len;//将当前字符串长度计数重置
    return this;//返回新字符串对象

}

2.reverse方法

public StringBuilder reverse() {undefined
super.reverse();//调用父类方法
return this;//返回当前对象
}

方法描述:将此字符序列用其反转形式取代。

该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:

public AbstractStringBuilder reverse() {undefined

    //是否含代理字符
    boolean hasSurrogates = false;
    int n = count - 1;  //定义一个变量表示长度-1

     //j初始化,长度-2再算术右移一位 j = (count-2)/2
    //偶数长度,遍历一半次数,对调替换
    //奇数长度,遍历一半-1次数,对调替换,中间值不用替换
    for (int j = (n-1) >> 1; j >= 0; j--) {undefined
        int k = n - j;
        char cj = value[j];
        char ck = value[k];
        value[j] = ck;
        value[k] = cj;
        if (Character.isSurrogate(cj) ||
            Character.isSurrogate(ck)) {undefined
            hasSurrogates = true;
        }
    }
    if (hasSurrogates) {undefined
        reverseAllValidSurrogatePairs();
    }
    return this;
}

3.delete方法

public StringBuilder delete(int start, int end) {//传入一个删除起始点和结束点
super.delete(start, end);//调用父类的delete方法
return this;//返回当前对象
}

方法描述: 移除此序列的子字符串中的字符。

该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:

public AbstractStringBuilder delete(int start, int end) {undefined
if (start < 0)//如果起始下标小于零
throw new StringIndexOutOfBoundsException(start);//抛出字符串角标越界异常
if (end > count)//如果结束点大于最大下标
end = count;//直接将结束点设为最大下标
if (start > end)//如果其实大于结束
throw new StringIndexOutOfBoundsException();//抛出字符串角标越界异常
int len = end - start;//定义变量存储起始下标和结束下标之间的长度
if (len > 0) {//如果删除长度大于零
System.arraycopy(value, start+len, value, start, count-end);//修改字符串数组
count -= len;//重置字符个数
}
return this;//返回当前对象
}

4.insert方法

public StringBuilder insert(int offset, Object obj) {//传入一个偏移量offset和一个字符串对象
super.insert(offset, obj);//调用父类的insert方法
return this;//返回当前对象
}

方法描述:将 Object 参数的字符串表示形式插入此字符序列中。

该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:

public AbstractStringBuilder insert(int offset, Object obj) {undefined
return insert(offset, String.valueOf(obj));//调用该类中的重载方法
}

public AbstractStringBuilder insert(int offset, String str) {//传入一个偏移量offset和一个字符串对象
if ((offset < 0) || (offset > length()))//如果偏移量小于0或者大于该串最长长度
throw new StringIndexOutOfBoundsException(offset);//丢出角标越界异常
if (str == null)//如果对象为空
str = “null”;//对象置为null
int len = str.length();//获取当前字符串对象的长度
ensureCapacityInternal(count + len);//扩充字符串数组长度
System.arraycopy(value, offset, value, offset + len, count - offset);//再原字符串对象数组中挪出位置
str.getChars(value, offset);//插入字符串
count += len;//更新当前字符串长度
return this;//返回当前对象
}

5.replace方法

public StringBuilder replace(int start, int end, String str) {//传入一个起始索引,一个终止索引,一个需要替换的字符串序列
super.replace(start, end, str);
return this;
}

方法描述:使用给定 String 中的字符替换此序列的子字符串中的字符。

该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:

public AbstractStringBuilder replace(int start, int end, String str) {undefined
if (start < 0)//如果起始索引小于字符串序列的最小索引
throw new StringIndexOutOfBoundsException(start);//丢出角标越界异常
if (start > count)//如果起始索引大于字符串序列的最大索引
throw new StringIndexOutOfBoundsException(“start > length()”);//丢出角标越界异常
if (start > end)//如果起始索引大于终止索引
throw new StringIndexOutOfBoundsException(“start > end”);//丢出角标越界异常

    if (end > count)如果终止索引大于原字符串最大索引
        end = count;//将终止索引转换为原字符串最大索引
    int len = str.length();//获取传入字符串的长度
    int newCount = count + len - (end - start);//设置修改后的字符串长度
    ensureCapacityInternal(newCount);//扩容

    System.arraycopy(value, end, value, start + len, count - end);//修改字符串数组
    str.getChars(value, start);//插入字符串
    count = newCount;//更新字符串长度
    return this;//返回当前对象
}

以下列表列出了 StringBuffer 类的其他常用方法:

序号方法描述
1public StringBuffer append(String s)将指定的字符串追加到此字符序列。
2public StringBuffer reverse()将此字符序列用其反转形式取代。
3public delete(int start, int end)移除此序列的子字符串中的字符。
4public insert(int offset, int i)将 int 参数的字符串表示形式插入此序列中。
5insert(int offset, String str)将 str 参数的字符串插入此序列中。
6replace(int start, int end, String str)使用给定 String 中的字符替换此序列的子字符串中的字符。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值