-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13. StringBuilder.java
More file actions
110 lines (91 loc) · 3.52 KB
/
13. StringBuilder.java
File metadata and controls
110 lines (91 loc) · 3.52 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Strings in Java are Immutable.
For any kind of modification in String we use String Builder.
Objects are created inside the Heap, and normal variables are created in stack.
StringBuilder: StringBuilder in Java is a mutable sequence of characters. It is a class that allows you to build a string by appending characters, strings, or other sequences of characters.
Unlike the String class, which is immutable, StringBuilder is designed for use as a drop-in replacement for StringBuffer in places where the latter is being used by a single thread (as is generally the case).
- StringBuilder objects can be modified after creation, whereas String objects are immutable.
- StringBuilder is not designed to be used in a multi-threaded environment. If you need thread safety, use StringBuffer instead.
- StringBuilder is faster and more memory-efficient than concatenating strings using the + operator, especially when building long strings or performing multiple concatenations.
- StringBuilder provides methods like append(), insert(), replace(), and delete() to modify the string sequence.
- StringBuilder implements the Serializable interface, making it possible to serialize and deserialize its contents.
*/
import java.util.*;
public class StringBuilders {
public static void main(String[] args) {
// declaration
StringBuilder sb = new StringBuilder("Aisha");
System.out.println(sb);
}
}
import java.util.*;
public class StringBuilders {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Aisha");
System.out.println(sb);
// character at index 0
System.out.println(sb.charAt(0));
}
}
import java.util.*;
public class StringBuilders {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Aisha");
System.out.println(sb);
// set character at index 0
sb.setCharAt(0, 'P');
System.out.println(sb);
}
}
import java.util.*;
public class StringBuilders {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Aisha");
System.out.println(sb);
// insert a character at some index
sb.insert(0, 'P');
System.out.println(sb);
}
}
import java.util.*;
public class StringBuilders {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Aisha");
System.out.println(sb);
// insert a character at some index
sb.insert(2, 'i');
System.out.println(sb);
// delete the extra 'i'
sb.delete(2, 3);
System.out.println(sb);
}
}
import java.util.*;
public class StringBuilders {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("A");
// append
sb.append("i");
sb.append("s");
sb.append("h");
sb.append("a");
System.out.println(sb);
}
}
/* Qs. Reverse a String (using StringBuilder class) */
import java.util.*;
public class ReverseString {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("aisha");
System.out.println("Before reverse: " + sb);
for(int i=0; i<sb.length()/2; i++) {
int front = i;
int back = sb.length()-1-i; // 5-1-0 = 4
char frontChar = sb.charAt(front);
char backChar = sb.charAt(back);
sb.setCharAt(front, backChar);
sb.setCharAt(back, frontChar);
}
System.out.println("After reverse: " + sb);
}
}