Java中StringBuilder的用法

  今天刷题,用到了一个之前从来没有用过的字符串类,学习一下和大家分享一下。在java中String、StringBuffer、StringBuilder是编程中经常使用的字符串类。

  我们先看一下String、StringBuffer、StringBuilder这三种字符串类的区别。

 1可变与不可变

  在String类中使用字符数组保存字符串,是final修饰,因此是不可变的:

private final char value[];

 对String对象的任何改变都不会影响到原来的对象,它一般是生成一个新的对象然后进行操作的。

  而 StringBuffer、StringBuilder继承AbstractStringBuilder类,他们是可变的:

char[] value;


  2是否多线程安全

  String中的对象是不可变的,是常量,因此线程安全。

  AbstractStringBuilder是StringBuilder与StringBuffer的公共父类,定义了一些字符串的基本操作,如expandCapacity、append、insert、indexOf等公共方法。StringBuffer对方法加了同步锁或者对调用的方法加了同步锁,因此它是线程安全的。StringBuilder并没有对方法进行加同步锁,因此它是线程不安全的。

  StringBuilder与StringBuffer有公共父类AbstractStringBuilder(抽象类),抽象类中可以定义一些子类的公共方法,子类只需要增加新的功能,不需要重复写已经存在的方法;而接口中只是对方法的申明和常量的定义。StringBuilder、StringBuffer的方法都会调用AbstractStringBuilder中的公共方法,如super.append(...)。只是StringBuffer会在方法上加synchronized关键字,进行同步。如果程序不是多线程的StringBuilder的效率高于StringBuffer。


 StringBuilder的用法

  String对象是不可改变的,每次使用System.String类中的方法之一的时候,都需要在内存中新建一个字符串对象,然后分配新的空间。使用System.Text.StringBuilder类可以修改字符串而不用创建新的对象。

  构造函数:

StringBuilder()
//创建一个空的字符串,默认大小为16个字符。

StringBuilder(CharSequence seq)
//建立一个和seq字符序列包含的字符一样的字符串

StringBuilder(int capacity)
//建议一个指定大小的字符串

StringBuilder(String str)
//建立一个以strzi字符串为内容的字符串


  常用的方法:


  StringBuilder append()

StringBuilder	append(boolean b)
Appends the string representation of the boolean argument to the sequence.

StringBuilder	append(char c)
Appends the string representation of the char argument to this sequence.

StringBuilder	append(char[] str)
Appends the string representation of the char array argument to this sequence.

StringBuilder	append(char[] str, int offset, int len)
Appends the string representation of a subarray of the char array argument to this sequence.

StringBuilder	append(CharSequence s)
Appends the specified character sequence to this Appendable.

StringBuilder	append(CharSequence s, int start, int end)
Appends a subsequence of the specified CharSequence to this sequence.

StringBuilder	append(double d)
Appends the string representation of the double argument to this sequence.

StringBuilder	append(float f)
Appends the string representation of the float argument to this sequence.

StringBuilder	append(int i)
Appends the string representation of the int argument to this sequence.

StringBuilder	append(long lng)
Appends the string representation of the long argument to this sequence.

StringBuilder	append(Object obj)
Appends the string representation of the Object argument.

StringBuilder	append(String str)
Appends the specified string to this character sequence.

StringBuilder	append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.

我们可以看到,StringBuilder可以吧任何类型的数据转换成字符串形式,人后加入到该对象的结尾处。


int capacity():

public int capacity(){
  //返回当前容量大小,即还可以存储多少字符
}


char charAt(int index):
public char charAt(int index){
// 返回指定index的char值
}

 StringBuilder insert():
StringBuilder	insert(int offset, boolean b)
Inserts the string representation of the boolean argument into this sequence.

StringBuilder	insert(int offset, char c)
Inserts the string representation of the char argument into this sequence.

StringBuilder	insert(int offset, char[] str)
Inserts the string representation of the char array argument into this sequence.

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

StringBuilder	insert(int dstOffset, CharSequence s)
Inserts the specified CharSequence into this sequence.

StringBuilder	insert(int dstOffset, CharSequence s, int start, int end)
Inserts a subsequence of the specified CharSequence into this sequence.

StringBuilder	insert(int offset, double d)
Inserts the string representation of the double argument into this sequence.

StringBuilder	insert(int offset, float f)
Inserts the string representation of the float argument into this sequence.

StringBuilder	insert(int offset, int i)
Inserts the string representation of the second int argument into this sequence.

StringBuilder	insert(int offset, long l)
Inserts the string representation of the long argument into this sequence.

StringBuilder	insert(int offset, Object obj)
Inserts the string representation of the Object argument into this character sequence.

StringBuilder	insert(int offset, String str)
Inserts the string into this character sequence.
将不同类型的数据转换为string,然后插入在指定位置上.
 
 
int length():
public int length(){
//返回当前含有的字符个数
}

StringBuilder replace():
StringBuilder	replace(int start, int end, String str)

Replaces the characters in a substring of this sequence with characters in the specified String.
将指定位置上的string替换为str.

StringBuilder reverse():
StringBuilder	reverse()
Causes this character sequence to be replaced by the reverse of the sequence.
将字符串序列颠倒
 
 
StringBuilder toString():
String	toString()
Returns a string representing the data in this sequence.
将数据以字符串的形式返回

 










  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值