2021-10-16 Java中StringBuffer的基本使用方法

参考资料:Oracle官方文档-StringBuffer

StringBuffer构造函数

//Constructor and Description
StringBuffer()
//Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer(CharSequence seq)
//Constructs a string buffer that contains the same characters as the specified CharSequence.
StringBuffer(int capacity)
//Constructs a string buffer with no characters in it and the specified initial capacity.
StringBuffer(String str)
//Constructs a string buffer initialized to the contents of the specified string.

方法

常用函数

StringBuffer sb = new StringBuffer();

//Appends the string representation of the argument to this sequence.
sb.append(123); // = sb.append(String.valueOf(123));
sb.append("abc");

//Returns the current capacity.
sb.capacity();
//Returns the length (character count).
sb.length(); 
//Returns a string representing the data in this sequence.
sb.toString();

//Returns the char value in this sequence at the specified index.
sb.charAt​(int index);

//Compares two StringBuffer instances lexicographically.
sb.compareTo​(new StringBuffer("123"));

//Removes the characters in a substring of this sequence.
sb.delete​(int start, int end)
//Removes the char at the specified position in this sequence.
sb.deleteCharAt​(int index)

//Causes this character sequence to be replaced by the reverse of the sequence.
sb.reverse();

不常用函数

//Ensures that the capacity is at least equal to the specified minimum.
void	ensureCapacity​(int minimumCapacity)	

//Characters are copied from this sequence into the destination character array dst.
void	getChars​(int srcBegin, int srcEnd, char[] dst, int dstBegin)	

//Returns the index within this string of the first occurrence of the specified substring.
int	indexOf​(String str)	

//Inserts the string representation of the argument into this sequence.
StringBuffer	insert​(int offset, boolean/char/char[]/int/double/...)
//Inserts the string representation of a subarray of the str array argument into this sequence.
StringBuffer	insert​(int index, char[] str, int offset, int len)		

//The character at the specified index is set to ch.
void	setCharAt​(int index, char ch)	
//Sets the length of the character sequence.
void	setLength​(int newLength)	

//Returns a new String that contains a subsequence of characters currently contained in this character sequence.
String	substring​(int start)
//Returns a new String that contains a subsequence of characters currently contained in this sequence.
String	substring​(int start, int end)	

其他资料

  • stringbuffer和stringbuilder哪个效率高,应该使用哪个?
    1. stringbuffer时线程安全的,因为他的append方法会有同步锁,而stringbuilder时线程不安全的
    2. 单线程的时候使用stringbuilder,在多线程的时候使用stringbuffer
  • StringBuffer和String的优缺点比较
    1. String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间。
    2. StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象。
    3. 一般情况下,速度从快到慢:StringBuilder>StringBuffer>String,这种比较是相对的,不是绝对的。
    4. 总结
      (1).如果要操作少量的数据用 = String
      (2).单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
      (3).多线程操作字符串缓冲区 下操作大量数据 = StringBuffer
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值