1. StringBufffer:线程安全的可变字符串
适用场景:对字符串操作时,每次拼接都会构成一个新的String对象,耗时也浪费时间,故可用StringBuffer可以解决这个问题。
2. StringBuffer和String的区别:前者长度和内容均可变,后者不可变
3. StringBuffer的构造方法:
- public StringBuffer():无参构造方法
- public StringBuffer(int capacity):指定容量的字符串缓冲区对象
- public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
4. StringBuffer()的方法:
- public int Capacity():返回当前容量 理论值
- public int length() :返回长度(字符数) 实际值
5. StringBuffer的常用功能
***添加功能:
- public StringBuffer append(String str):把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
- public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
***删除功能:(包左不包右)
- public StringBuffer deleteCharAt(int index):删除指定位置字符,并返回本身
- public StringBuffer delete(int start,int end):删除从指定位置开始,并从指定位置结束字符,并返回本身
***截取功能:(返回值类型不是StringBuffer)
- public String substring(int start)
- public String substring(int start,int end)
***替换功能:
- public StringBuffer replace(int start,int end,String str),从start到end用str替换
***反转功能:
- public StringBuffer reverse()
***参考引用:优快云博主「围巢111」的原创文章,遵循CC 4.0 BY-SA版权协议,附上原文出处链接及本声明。
***原文链接:https://blog.youkuaiyun.com/qq_40817827/article/details/89313998