-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringBuilder.java
More file actions
43 lines (36 loc) · 1.79 KB
/
stringBuilder.java
File metadata and controls
43 lines (36 loc) · 1.79 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
//Question: ALL ABOUT STRING BUILDER
package stringBuilder;
import java.util.Scanner;
public class stringBuilder {
public static void main(String[] args) {
//Initialisation
String s = "Raj Sarkar"; // (String)
StringBuilder sb = new StringBuilder("abc"); // (String builder)
System.out.println(sb);
//Built in functions
System.out.println(sb.length()); // prints length
StringBuilder xy = new StringBuilder(""); // creating an empty string builder
System.out.println(xy.length()); // Output: 0
System.out.println(xy.capacity()); // prints capacity. initially, the capacity of an empty string builder is 16
System.out.println(sb.capacity()); // Output: initial capacity + number of character = 19
StringBuilder ab = new StringBuilder(10); // we can also initialise a string builder with a given capacity
System.out.println(ab.capacity()); // Output: 10
System.out.println(sb.compareTo(xy)); // Output: 3 (Because xy is an empty String Builder)
System.out.println(sb.reverse()); // Output: cba
System.out.println();
// baki jo jo string main tha wo sab yaha bhi hai
//Taking input
Scanner sc = new Scanner(System.in);
StringBuilder rs = new StringBuilder(sc.nextLine()); // Method 1
System.out.println(rs);
System.out.println();
String t ;
t = sc.nextLine();
StringBuilder tb = new StringBuilder(t); // Method 2
System.out.println(tb);
System.out.println();
//setCharAt()
sb.setCharAt(2, 'd'); // Here we can change individual index without creating anything new
System.out.println(sb); // Output: "cbd"
}
}