stringbuffer和stringbuilder 如何使用,请多给出几个代码实例
时间: 2024-05-12 09:16:26 浏览: 17
Sure thing! Here are a few code examples for using StringBuffer and StringBuilder:
1. Using StringBuffer:
```
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
String result = sb.toString(); // result = "Hello World"
```
2. Using StringBuilder:
```
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
String result = sb.toString(); // result = "Hello World"
```
3. StringBuffer vs StringBuilder:
```
String str = "Hello";
StringBuffer sb = new StringBuffer(str); // or StringBuilder
sb.append(" World");
String result = sb.toString(); // result = "Hello World"
str.concat(" World"); // str is still "Hello"
```
Hope this helps! Let me know if you have any other questions.
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)