-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathStringMutableDemo.java
More file actions
37 lines (29 loc) · 1.02 KB
/
StringMutableDemo.java
File metadata and controls
37 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.CharArrayWriter;
import java.io.IOException;
public class StringMutableDemo {
public static void main(String[] args) throws IOException {
// Approach 1
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("World");
stringBuilder.insert(0,"Hello ");
stringBuilder.deleteCharAt(4);
stringBuilder.insert(4,'o');
stringBuilder.setLength(5);
stringBuilder.reverse();
String str = stringBuilder.toString();
// Approach 2
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("World");
stringBuffer.insert(0,"Hello ");
stringBuffer.deleteCharAt(4);
stringBuffer.insert(4,'o');
stringBuffer.setLength(5);
stringBuffer.reverse();
String str1 = stringBuffer.toString();
// Approach 3
CharArrayWriter cw = new CharArrayWriter();
cw.write("Hello");
cw.write(" World");
String string3 = cw.toString();
}
}