在Java中,StringBuilder类是一个可变对象,用于处理字符串。它提供了许多方法来操作字符串,以下是其中一些常用的方法:
- append(String str) 将指定的字符串追加到此字符串构建器的末尾。也可以是单个字符、整数、浮点数、
-
StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); // appends " World" to the end of "Hello"
- delete(int start, int end) 从此字符串构建器中删除从start到end的子字符串。
-
StringBuilder sb = new StringBuilder("Hello World"); sb.delete(5, 10); // deletes the characters from index 5 to index 10 (inclusive), 得到Hellold
- deleteCharAt(int index) 从此字符串构建器中删除指定位置的字符。
-
StringBuilder sb = new StringBuilder("Hello World"); sb.deleteCharAt(7); // deletes the character at index 7,得到Hello, orld
- insert(int index, String str) 将指定的字符串插入到此字符串构建器的指定索引处。也可以插入单个字符、整数、浮点数
-
StringBuilder sb = new StringBuilder("Hello, world!"); sb.insert(7,"hhh"); //输出Hello, hhhworld
- replace(int start, int end, String str) 用指定的字符串替换此字符串构建器中从start到end的子字符串。
-
StringBuilder sb = new StringBuilder("Hello World"); sb.replace(6, 11, "Planet"); // replaces "World" with "Planet" in the string, resulting in "Hello Planet"
- reverse() 反转此字符串构建器中的字符顺序。
-
StringBuilder sb = new StringBuilder("Hello World"); sb.reverse(); // reverses the string to "dlroW olleH"
- toString() 返回此字符串构建器中的字符串。
-
String str = sb.toString(); System.out.println(str); // 输出: Hello, world!
- length() 返回此字符串构建器中的字符数。
-
StringBuilder sb = new StringBuilder("Hello, world!"); int len = sb.length(); //输出12
- capacity() 返回此字符串构建器的容量
-
StringBuilder sb = new StringBuilder("Hello, world!"); System.out.println(sb.capacity()); // 输出: 16 //这是因为在默认情况下,StringBuilder 实例的容量是 16。如果需要更多空间,可以调用 ensureCapacity() 方法预先分配足够的空间。
- ensureCapacity(int minCapacity) 如果需要,增加此字符串构建器的容量,以便它至少可以容纳由参数指定的最小容量。
-
StringBuilder sb = new StringBuilder("Hello, world!"); sb.ensureCapacity(20); // 增加容量至少为20
- charAt(int index) 返回此字符串构建器中指定索引处的字符。
-
StringBuilder sb = new StringBuilder("Hello, world!"); char c = sb.charAt(5); // c 会得到字符 'o'
- substring(int start, int end) 返回此字符串构建器中从start到end的子字符串。
-
StringBuilder sb = new StringBuilder("Hello, world!"); String sub = sb.substring(2, 7); // sub 会得到子字符串 "llo, "
- indexOf(String str) 返回指定子字符串在此字符串构建器中第一次出现的索引。
-
StringBuilder sb = new StringBuilder("Hello, world!"); int index = sb.indexOf("world"); // index 会得到7,因为 "world" 从索引7开始
- lastIndexOf(String str) 返回指定子字符串在此字符串构建器中最后一次出现的索引。
-
StringBuilder sb = new StringBuilder("Hello, world world!"); int lastIndex = sb.lastIndexOf("world"); // lastIndex 会得到13
- indexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串在此字符串构建器中第一次出现的索引。
-
StringBuilder sb = new StringBuilder("Hello, world!"); // 查找子字符串 "world" 从索引位置 7 开始的第一次出现 int index = sb.indexOf("world", 7); System.out.println(index); // 输出: 7
- lastIndexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串在此字符串构建器中最后一次出现的索引。
-
StringBuilder sb = new StringBuilder("Hello, world!"); int lastindex = sb.lastIndexOf("world", 7); System.out.println(lastindex); // 输出: 7
- equals(Object anObject) 将此字符串构建器与指定的对象比较。
-
StringBuilder sb1 = new StringBuilder("Hello, World!"); StringBuilder sb2 = new StringBuilder("Hello, World!"); StringBuilder sb3 = new StringBuilder("Hello, Java!"); System.out.println(sb1.equals(sb2)); // 输出: true System.out.println(sb1.equals(sb3)); // 输出: false
- equalsIgnoreCase(String anotherString) 按字典顺序比较此字符串构建器和另一个字符串,不区分大小写,不等时为false,否则为true。
-
StringBuilder sb1 = new StringBuilder("Hello, World!"); StringBuilder sb2 = new StringBuilder("hello, world!"); System.out.println(sb1.equalsIgnoreCase(sb2)); // 输出: true