-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringsBuilder.java
44 lines (34 loc) · 990 Bytes
/
stringsBuilder.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
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
public class stringsBuilder {
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
System.out.println(sb);
//char at index 0
System.out.println(sb.charAt(0));
//set character at index
sb.setCharAt(1, 'L');
System.out.println(sb);
//INSERT
sb.insert(0, 'B');
System.out.println(sb);
//Delete
sb.delete(0, 2);
System.out.println(sb);
//append
sb.append(" ");
sb.append("W");
sb.append("o");
System.out.println(sb);
System.out.println(sb.length());
//reverse a string
for (int i=0; i<sb.length()/2;i++){
int front=i;
int back=sb.length()-1-i;//5-1-0
char frontchar=sb.charAt(front);
char backchar=sb.charAt(back);
sb.setCharAt(front, backchar);
sb.setCharAt(back, frontchar);
}
System.out.println(sb);
}
}