forked from puruagarwal1/hacktoberfest-2022-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBufferMethods.java
More file actions
69 lines (56 loc) · 2.21 KB
/
StringBufferMethods.java
File metadata and controls
69 lines (56 loc) · 2.21 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
public class StringBufferMethods {
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("Java Coding");
//1 length()
System.out.println(sb.length());
//2 capacity()
System.out.println(sb.capacity());
//3 charAt(int index)
System.out.println(sb.charAt(8));
//System.out.println(sb.charAt(90));
//4 setCharAt(int index,char ch)
sb.setCharAt(0, 'Y');
System.out.println(sb);
//5 append(String s) in string place we can use any primitive data type
sb.append(true);
sb.append(123);
sb.append(123.123);
sb.append('w');
System.out.println(sb);
//6 insert(int index ,String s) same as append()
//but it is on specified location
sb.insert(0,"Learn ");
sb.insert(11,false);
sb.insert(16, 1829.299);
System.out.println(sb);
//7 delete(int begin,int end) delete certain words
sb.delete(16, 24);
System.out.println(sb);
//8 deleteCharAt(int index) delete specified index
sb.deleteCharAt(11);
System.out.println(sb);
//9 reverse() reverse all characters
sb.reverse();
System.out.println(sb);
sb.reverse();
//11 setLength() only specified no of characters and remove
///all the remaining characters.
sb.setLength(10);
System.out.println(sb);
//12 trimToSize() To deallocate the extra allocated free memory
//such that capacity and size are equal.
StringBuffer sb1=new StringBuffer(1000);
System.out.println(sb1.capacity());
sb1.append("OM");
System.out.println(sb1.capacity());
sb1.trimToSize();
System.out.println(sb1.capacity());
//13 ensureCapacity(int initialcapacirty);
//To increase the capacity dynamically(fly) based on our requirement
StringBuffer sb2=new StringBuffer();
System.out.println(sb.capacity());//16
sb2.ensureCapacity(1000);
System.out.println(sb2.capacity());//1000
}
}