stringbuffer和stringbuilder 如何使用,请多给出几个代码实例
时间: 2024-05-12 09:16:26 浏览: 76
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.
阅读全文