Java StringBuilder和StringBuffer用法

╰半橙微兮° 2022-09-30 12:49 217阅读 0赞

StringBuilderStringBufferString类的同伴类。它们表示一个可变的字符序列。StringBuffer是线程安全的,StringBuilder不是线程安全的。

两个类都有相同的方法,除了StringBuffer中的所有方法都是同步的。StringBuilder对象是可修改的字符串。StringBuilder类包含四个构造函数:

  1. StringBuilder() StringBuilder(CharSequence seq) StringBuilder(int capacity) StringBuilder(String str)
  2. Java

无参数构造函数创建一个默认容量为16的空StringBuilder对象。第二个构造函数使用CharSequence对象作为参数。它创建一个StringBuilder对象,其内容与指定的CharSequence相同。

第三个构造函数使用int作为参数; 它创建一个空的StringBuilder对象,其初始容量与指定的参数相同。

以下是创建StringBuilder对象的一些示例:

  1. StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("Here is the content"); StringBuilder sb3 = new StringBuilder(200);
  2. Java

append()方法将文本添加到StringBuilder的结尾处。它可使用多种类型的参数。insert()delete()用于修改字符串的内容。

长度和容量

StringBuilder类有两个属性:lengthcapacity。它的长度是指其内容的长度,而其容量是指它可以容纳而不分配新的内存的最大字符数。length()capacity()方法分别返回其长度和容量。例如,

  1. public class Main { public static void main(String[] args) { StringBuilder sb = new StringBuilder(200); // Capacity:200, length:0 sb.append("Hello"); // Capacity:200, length:5 int len = sb.length(); // len is assigned 5 int capacity = sb.capacity(); // capacity is assigned 200 } }
  2. Java

转换为字符串

可以通过使用toString()方法将StringBuilder的内容作为String类型的字符串值。

  1. public class Main { public static void main(String[] args) { // Create a String object String s1 = new String("Hello"); // Create a StringBuilder from of the String object s1 StringBuilder sb = new StringBuilder(s1); // Append " Java" to the StringBuilder's content sb.append(" Java"); // Now, sb contains "Hello Java" // Get a String from the StringBuilder String s2 = sb.toString(); // s2 contains "Hello Java" } }
  2. Java

StringBuilder有一个setLength()方法,它的新长度作为参数。如果新长度大于旧长度,则额外位置(多过的部分)用空字符填充(空字符为\u0000)。

如果新长度小于旧长度,则其内容将被截断以适应新长度。

  1. public class Main { public static void main(String[] args) { // Length is 5 StringBuilder sb = new StringBuilder("Hello"); // Now the length is 7 with last two characters as null character '\u0000' sb.setLength(7); // Now the length is 2 and the content is "He" sb.setLength(2); } }
  2. Java

示例

StringBuilder类有一个reverse()方法,它用相同的字符序列替换其内容,但顺序相反。以下代码显示了StringBuilder类的一些方法的使用。

  1. public class Main { public static void main(String[] args) { // Create an empty StringBuffer StringBuilder sb = new StringBuilder(); printDetails(sb); // Append "good" sb.append("good"); printDetails(sb); // Insert "Hi " in the beginning sb.insert(0, "Hi "); printDetails(sb); // Delete the first o sb.deleteCharAt(1); printDetails(sb); // Append " be with you" sb.append(" be with you"); printDetails(sb); // Set the length to 3 sb.setLength(3); printDetails(sb); // Reverse the content sb.reverse(); printDetails(sb); } public static void printDetails(StringBuilder sb) { System.out.println("Content: \"" + sb + "\""); System.out.println("Length: " + sb.length()); System.out.println("Capacity: " + sb.capacity()); // Print an empty line to separate results System.out.println(); } }
  2. Java

上面的代码生成以下结果。

  1. Content: "" Length: 0 Capacity: 16 Content: "good" Length: 4 Capacity: 16 Content: "Hi good" Length: 7 Capacity: 16 Content: "H good" Length: 6 Capacity: 16 Content: "H good be with you" Length: 20 Capacity: 34 Content: "H g" Length: 3 Capacity: 34 Content: "g H" Length: 3 Capacity: 34
  2. Java

字符串连接运算符(+)

在开发过程中,也经常使用+运算符将字符串,原始类型值或对象连接成另一个字符串。

例如,

  1. String str = "X" + "Y" + 12.56;
  2. Java

为了优化字符串连接操作,编译器用一个使用StringBuilder的语句替换字符串连接。

  1. String str = new StringBuilder().append("X").append("Y").append(12.56).toString();
  2. Java

发表评论

表情:
评论列表 (有 0 条评论,217人围观)

还没有评论,来说两句吧...

相关阅读