-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringBufferBuilderExamples.java
33 lines (26 loc) · 1.17 KB
/
StringBufferBuilderExamples.java
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
package concept.examples.string;
public class StringBufferBuilderExamples {
public static void main(String[] args) {
// StringBuffer and StringBuilder are used when you want to modify
// object values.
StringBuffer stringbuffer = new StringBuffer("12345");
stringbuffer.append("6789");
System.out.println(stringbuffer); // 123456789
// All StringBuffer methods modify the value of the object.
StringBuilder sb = new StringBuilder("0123456789");
// StringBuilder delete(int startIndex, int endIndexPlusOne)
System.out.println(sb.delete(3, 7));// 012789
StringBuilder sb1 = new StringBuilder("abcdefgh");
// StringBuilder insert(int indext, String whatToInsert)
System.out.println(sb1.insert(3, "ABCD"));// abcABCDdefgh
StringBuilder sb2 = new StringBuilder("abcdefgh");
// StringBuilder reverse()
System.out.println(sb2.reverse());// hgfedcba
// Similar functions exist in StringBuffer also
// All functions also return a reference to the object after modifying
// it.
// This allows a concept called method chaining.
StringBuilder sb3 = new StringBuilder("abcdefgh");
System.out.println(sb3.reverse().delete(5, 6).insert(3, "---"));// hgf---edba
}
}