java StringBuilder 类含有sting类哪学方法
时间: 2024-06-02 12:10:09 浏览: 10
Java的StringBuilder类确实包含了一些String类中的方法,例如:
1. length():返回StringBuilder对象中字符序列的长度。
2. charAt(int index):返回指定索引处的字符。
3. subSequence(int start, int end):返回一个新的CharSequence对象,该对象是当前StringBuilder对象中的一部分。
4. toString():将StringBuilder对象转换为String对象。
5. indexOf(String str):返回指定字符串在StringBuilder对象中第一次出现的索引。
6. equals(Object obj):比较StringBuilder对象与指定对象是否相等。
7. toLowerCase():将StringBuilder对象中的所有字符转换为小写。
8. toUpperCase():将StringBuilder对象中的所有字符转换为大写。
9. trim():去除StringBuilder对象中首尾的空白字符。
需要注意的是,虽然StringBuilder类包含了一些String类中的方法,但是它并不是String类的子类。StringBuilder类是一个独立的类,它提供了一些方便操作字符串的方法,例如在字符串中插入、删除、替换字符等。
相关问题
java StringBuilder 类实现方法实例
StringBuilder 类是一个可变的字符串类,可以通过它来进行字符串的拼接、插入、替换等操作。以下是几个常用的方法实例。
1. append() 方法,用于字符串拼接。
```
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
System.out.println(sb.toString()); // 输出 Hello World
```
2. insert() 方法,用于在指定位置插入字符串。
```
StringBuilder sb = new StringBuilder();
sb.append("Hello World");
sb.insert(5, ",");
System.out.println(sb.toString()); // 输出 Hello, World
```
3. replace() 方法,用于替换指定位置的字符串。
```
StringBuilder sb = new StringBuilder();
sb.append("Hello World");
sb.replace(6, 11, "Java");
System.out.println(sb.toString()); // 输出 Hello Java
```
4. delete() 方法,用于删除指定位置的字符串。
```
StringBuilder sb = new StringBuilder();
sb.append("Hello World");
sb.delete(5, 11);
System.out.println(sb.toString()); // 输出 Hello
```
5. reverse() 方法,用于反转字符串。
```
StringBuilder sb = new StringBuilder();
sb.append("Hello World");
sb.reverse();
System.out.println(sb.toString()); // 输出 dlroW olleH
```
java stringbuilder类的常用方法
Java StringBuilder类的常用方法包括:append()、insert()、delete()、replace()、charAt()、length()、indexOf()、substring()等。它们可以用于字符串的拼接、剪切、替换、查询等常见操作。StringBuilder对象可变,因此适用于需要频繁修改字符串内容的场景,比如动态生成长字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)