Java中的StringBuffer类

StringBuffer class is used to create a mutable string object. It means, it can be changed after it is created. It represents growable and writable character sequence.

StringBuffer类用于创建可变的字符串对象。 这意味着,它可以在创建后进行更改。 它代表可增长和可写的字符序列。

It is similar to String class in Java both are used to create string, but stringbuffer object can be changed.

它类似于Java中的String类,都用于创建字符串,但是可以更改stringbuffer对象。

So StringBuffer class is used when we have to make lot of modifications to our string. It is also thread safe i.e multiple threads cannot access it simultaneously. StringBuffer defines 4 constructors.

因此,当我们必须对字符串进行大量修改时,将使用StringBuffer类。 它也是线程安全的,即多个线程不能同时访问它。 StringBuffer定义了4个构造函数。

  1. StringBuffer(): It creates an empty string buffer and reserves space for 16 characters.

    StringBuffer ():它将创建一个空的字符串缓冲区,并保留16个字符的空间。

  2. StringBuffer(int size): It creates an empty string and takes an integer argument to set capacity of the buffer.

    StringBuffer (整数大小):创建一个空字符串,并使用一个整数参数来设置缓冲区的容量。

  3. StringBuffer(String str): It creates a stringbuffer object from the specified string.

    StringBuffer (String str):它从指定的字符串创建一个stringbuffer对象。

  4. StringBuffer(charSequence []ch): It creates a stringbuffer object from the charsequence array.

    StringBuffer (charSequence [] ch):它从charsequence数组创建一个stringbuffer对象。

示例:创建一个StringBuffer对象 (Example: Creating a StringBuffer Object)

In this example, we are creating string buffer object using StrigBuffer class and also testing its mutability.

在此示例中,我们将使用StrigBuffer类创建字符串缓冲区对象,并测试其可变性。

public class Demo {

	public static void main(String[] args) {

		StringBuffer sb = new StringBuffer("study");
		System.out.println(sb);
		// modifying object
		sb.append("tonight");
		System.out.println(sb);    // Output: studytonight

	}
}

study studytonight

今晚学习

示例:String和StringBuffer之间的区别 (Example: difference between String and StringBuffer)

In this example, we are creating objects of String and StringBuffer class and modifying them, but only stringbuffer object get modified. See the below example.

在此示例中,我们将创建String和StringBuffer类的对象并对其进行修改,但是只有stringbuffer对象会被修改。 请参见以下示例。

class Test {
 public static void main(String args[])
 {
  String str = "study";
  str.concat("tonight");
  System.out.println(str);      // Output: study

  StringBuffer strB = new StringBuffer("study");
  strB.append("tonight");
  System.out.println(strB);    // Output: studytonight
 }
}

study studytonight

今晚学习

说明: (Explanation:)

Output is such because String objects are immutable objects. Hence, if we concatenate on the same String object, it won't be altered But StringBuffer creates mutable objects. Hence, it can be altered.

输出是这样的,因为String对象是不可变的对象。 因此,如果我们在同一个String对象上串联,它将不会被更改,但是StringBuffer会创建可变对象。 因此,可以对其进行更改。

StringBuffer类的重要方法 (Important methods of StringBuffer class)

The following methods are some most commonly used methods of StringBuffer class.

以下方法是StringBuffer类的一些最常用方法。

附加() (append())

This method will concatenate the string representation of any type of data to the end of the StringBuffer object. append() method has several overloaded forms.

此方法会将任何类型的数据的字符串表示形式连接到StringBuffer对象的末尾。 append()方法具有几种重载形式。

StringBuffer append(String str)
StringBuffer append(int n)
StringBuffer append(Object obj)

The string representation of each parameter is appended to StringBuffer object.

每个参数的字符串表示形式都将附加到StringBuffer对象。

public class Demo {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("test");
		str.append(123);
		System.out.println(str);
	}
}

test123

测试123

插() (insert())

This method inserts one string into another. Here are few forms of insert() method.

此方法将一个字符串插入另一个字符串。 这是insert()方法的几种形式。

StringBuffer insert(int index, String str)
StringBuffer insert(int index, int num)
StringBuffer insert(int index, Object obj)

Here the first parameter gives the index at which position the string will be inserted and string representation of second parameter is inserted into StringBuffer object.

在这里,第一个参数给出了将在其中插入字符串的索引,第二个参数的字符串表示形式将插入到StringBuffer对象中。

public class Demo {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("test");
		str.insert(2, 123);
		System.out.println(str);
	}
}

test123

测试123

逆转() (reverse())

This method reverses the characters within a StringBuffer object.

此方法反转StringBuffer对象中的字符。

public class Demo {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("Hello");
		str.reverse();
		System.out.println(str);
	}
}

olleH

奥尔

更换() (replace())

This method replaces the string from specified start index to the end index.

此方法将字符串从指定的起始索引替换为结束索引。

public class Demo {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("Hello World");
		str.replace( 6, 11, "java");
		System.out.println(str);
	}
}

Hello java

你好Java

容量() (capacity())

This method returns the current capacity of StringBuffer object.

此方法返回StringBuffer对象的当前容量。

public class Demo {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer();
		System.out.println( str.capacity() );
	}
}

16

16

Note: Empty constructor reserves space for 16 characters. Therefore the output is 16.

注意:空的构造函数保留16个字符的空间。 因此输出为16。

sureCapacity() (ensureCapacity())

This method is used to ensure minimum capacity of StringBuffer object.

此方法用于确保StringBuffer对象的最小容量。

If the argument of the ensureCapacity() method is less than the existing capacity, then there will be no change in existing capacity.

如果suresureCapacity()方法的参数小于现有容量,则现有容量不会发生变化。

If the argument of the ensureCapacity() method is greater than the existing capacity, then there will be change in the current capacity using following rule: newCapacity = (oldCapacity*2) + 2.

如果suresureCapacity()方法的参数大于现有容量,则将使用以下规则更改当前容量: newCapacity =(oldCapacity * 2)+ 2

public class Demo {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer();
		System.out.println( str.capacity()); //output: 16 (since empty constructor reserves space for 16 characters)
		str.ensureCapacity(30); //greater than the existing capacity
		System.out.println( str.capacity()); //output: 34 (by following the rule - (oldcapacity*2) + 2.) i.e (16*2)+2 = 34.
	}
}

16 34

16 34

翻译自: https://www.studytonight.com/java/stringbuffer-class.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值