-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringDemo.java
97 lines (82 loc) · 2.87 KB
/
stringDemo.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
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
public class stringDemo {
public static void main(String[] args) {
//methods of strings
System.out.println("Methods of Strings");
//length
String sl=new String("Hello World");
System.out.println(sl.length());
//substring
String sub=new String("Welcome");
System.out.println(sub.substring(2));
//String Comparison
String s1="Hello";//ascii of l to be collected
String s2="Heldo";//ascii of d to be collected
//value of d will be subtracted from l
System.out.println(s1.compareTo(s2));//subtracted value
//IsEmpty
String s4="";
System.out.println(s4.isEmpty());
//toLowerCase
String s5="Hello";
System.out.println(s1.toLowerCase());
//replace
String s6="Heldo";
String replace=s2.replace('d', 'l');
System.out.println(replace);
//equals
String x="Welcome to Java";
String y="WeLcOmE tO JaVa";
System.out.println(x.equals(y));
System.out.println("\n");
System.out.println("Creating StringBuffer");
//Creating StringBuffer and append method
StringBuffer s=new StringBuffer("Welcome to Java!");
s.append("Enjoy your learning");
System.out.println(s);//Welcome to Java! Enjoy your learning
//insert method
s.insert(0, 'w');
System.out.println(s);
//replace method
StringBuffer sb=new StringBuffer("Hello");
sb.replace(0, 2, "hEl");
System.out.println(sb);
//delete method
sb.delete(0, 1);
System.out.println(sb);
//StringBuilder
System.out.println("\n");
System.out.println("Creating StringBuilder");
StringBuilder sb1=new StringBuilder("Happy");
sb1.app
//conversion
System.out.println("\n");
System.out.println("Conversion of Strings to StringBuffer and StringBuilder");
String str = "Hello";
// conversion from String object to StringBuffer
StringBuffer sbr = new StringBuffer(str);
sbr.reverse();
System.out.println("String to StringBuffer");
System.out.println(sbr);
// conversion from String object to StringBuilder
StringBuilder sbl = new StringBuilder(str);
sbl.append("world");
System.out.println("String to StringBuilder");
System.out.println(sbl);
}
}
//conversion
System.out.println("\n");
System.out.println("Conversion of Strings to StringBuffer and StringBuilder");
String str = "Hello";
// conversion from String object to StringBuffer
StringBuffer sbr = new StringBuffer(str);
sbr.reverse();
System.out.println("String to StringBuffer");
System.out.println(sbr);
// conversion from String object to StringBuilder
StringBuilder sbl = new StringBuilder(str);
sbl.append("world");
System.out.println("String to StringBuilder");
System.out.println(sbl);
}
}