Java中的StringBuffer类

一、构造函数

    · public StringBuffer() :无参构造方法
    · public StringBuffer(int capacity) :指定容量的字符串缓冲区对象
    · public StringBuffer(String str) :指定字符串内容的字符串缓冲区对象(16 + str.length())
    StringBuffer(CharSequence seq)
    Constructs a string buffer that contains the same characters as the specified CharSequence.
 StringBuffer的方法:
 · public int capacity():返回当前容量,理论值
 · public int length():返回长度(字符数),实际值

二、方法

1)添加功能
· public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
· public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

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

3)替换功能
· public StringBuffer replace(int start,int end,String str):使用给定String中的字符替换词序列的子字符串中的字符

4)反转功能
· public StringBuffer reverse():将此字符序列用其反转形式取代,返回对象本身

5)截取功能
· public String substring(int start):返回一个新的String,它包含此字符序列当前所包含的字符子序列
· public String substring(int start,int end):返回一个新的String,它包含此序列当前所包含的字符子序列
注意:截取功能和前面几个功能的不同返回值类型是String类型,本身没有发生改变

下面的列表里的方法和 String 类的方法类似:
1 int capacity()
返回当前容量。
2 char charAt(int index)
返回此序列中指定索引处的 char 值。
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)
返回最后一次出现的指定子字符串在此字符串中的索引。
9 int length()
返回长度(字符数)。
10 void setCharAt(int index, char ch)
将给定索引处的字符设置为 ch。
11 void setLength(int newLength)
设置字符序列的长度。
12 CharSequence subSequence(int start, int end)
返回一个新的字符序列,该字符序列是此序列的子序列。
13 String substring(int start)
返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
14 String substring(int start, int end)
返回一个新的 String,它包含此序列当前所包含的字符子序列。
15 String toString()
返回此序列中数据的字符串表示形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值