Skip to content

Commit 58b1b38

Browse files
rtomarrtomar
authored andcommitted
String Class Examples
1 parent 297e41e commit 58b1b38

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.rt.string;
2+
import java.util.StringTokenizer;
3+
4+
import com.rt.cloning.Employee;
5+
6+
7+
public class forStrings {
8+
9+
/**
10+
* @param args
11+
*/
12+
public static void main(String[] args) {
13+
char ch[]={'h','e','l','l','o'};
14+
15+
System.out.println(ch);
16+
17+
String str=new String(ch);
18+
System.out.println(str);
19+
20+
System.out.println(str.equals(ch));
21+
22+
23+
String s1="Example of being immutable";
24+
String s2="Example of being immutable";//
25+
System.out.println("Checking s1 & s2 "+(s1==s2));
26+
27+
String s3=new String("Example of being immutable");//
28+
System.out.println("checking s1 & s3 "+(s1==s3));
29+
System.out.println("Checking s1 & s2"+s1.equals(s2)+"\nchecking s1 & s3"+s1.equals(s3));
30+
31+
System.out.println(s1.compareTo(s2));
32+
System.out.println(s1.compareTo(s3));
33+
34+
35+
s3=s3.intern();
36+
System.out.println("checking s1 & s3 "+(s1==s3));
37+
38+
String s41="abc";
39+
String s51="abc";
40+
String s61="abcdef";
41+
System.out.println(s41==s51);//true
42+
String s71=new String("abc");
43+
s71=s71+"def";
44+
System.out.println(s71);//abcdef
45+
System.out.println(s61);//abcdef
46+
// s71=s71.intern();
47+
System.out.println(s61==s71);//false
48+
//s51.concat("def");System.out.println("hghgj"+s51);
49+
StringBuffer tt=new StringBuffer("abc");
50+
tt.append("defssss");
51+
52+
System.out.println(tt);
53+
54+
55+
56+
57+
58+
59+
//String s1="Example of being immutable";
60+
System.out.println(s1.charAt(2));
61+
System.out.println(s1.length());
62+
System.out.println(s1.format("Hey there %d and %d results in %d", 2,3,5));
63+
System.out.println(s1.substring(4));
64+
System.out.println(s1.substring(4, 9));
65+
System.out.println(s2.substring(0, 1));//start index end index
66+
System.out.println(s1.contains("bein"));
67+
System.out.println(s1.isEmpty());
68+
69+
String s4="Hi this is";
70+
String s5="CCVT at UPES";
71+
String s6=s4+s5;// String s6=(new StringBuilder()).append(s4).append(s5).toString();
72+
System.out.println(s6);
73+
s4.concat(s5);//being immutable
74+
System.out.println(s4);
75+
s4=s4.concat(s5);
76+
System.out.println(s4);
77+
78+
s1.replace('o', 'q');
79+
System.out.println(s1);
80+
81+
s1=s1.replace("being", "not");
82+
System.out.println(s1);
83+
String s7=" after 5 space";
84+
System.out.println(s7);
85+
System.out.println(s7.trim());
86+
87+
System.out.println(s1.toLowerCase());
88+
System.out.println(s1.toUpperCase());
89+
90+
System.out.println(s1.startsWith("Ex"));
91+
System.out.println(s1.endsWith("le"));
92+
93+
int a=10;
94+
String s8=String.valueOf(a);
95+
96+
System.out.println(s8+12);
97+
98+
99+
////******************STRING BUFFER
100+
/*
101+
*/
102+
System.out.println("****************STRING BUFFER NOW***************");
103+
StringBuffer sf1=new StringBuffer();//can be passed with int or string
104+
//sf1.append("aaaaaaaaaaaaaaaaa");
105+
System.out.println(sf1.capacity()+""+sf1);
106+
sf1.append("few string to append");
107+
System.out.println(sf1);
108+
sf1.insert(0, "see more appended ");
109+
System.out.println(sf1);
110+
sf1.replace(0, 8, "deleted");
111+
System.out.println(sf1);
112+
sf1.delete(0, 7);
113+
System.out.println(sf1);
114+
sf1.reverse();
115+
System.out.println(sf1);
116+
// sf1.ensureCapacity(50);
117+
System.out.println(sf1.capacity());
118+
119+
StringBuilder sb1=new StringBuilder();//can be passed with int or string
120+
121+
sb1.reverse();
122+
System.out.println(sb1.capacity()+""+sb1);
123+
sb1.append("few string to append");
124+
System.out.println(sb1);
125+
sb1.insert(0, "see more appended ");
126+
System.out.println(sb1);
127+
sb1.replace(0, 8, "deleted");
128+
System.out.println(sb1);
129+
sb1.delete(0, 7);
130+
System.out.println(sb1);
131+
sb1.reverse();
132+
System.out.println(sb1);
133+
// sb1.ensureCapacity(500);
134+
System.out.println(sb1.capacity());
135+
136+
137+
// toString method tgo be explained
138+
String s9="This, string, will, be, tokenized";
139+
StringTokenizer stk=new StringTokenizer(s9,"s");
140+
while(stk.hasMoreTokens())
141+
{
142+
System.out.println(stk.nextToken());
143+
}
144+
145+
String s10[]=s9.split(",");
146+
System.out.println(s10.length);
147+
System.out.println(s10[0]+" XX"+s10[1]+" XX"+s10[2]);
148+
149+
150+
151+
152+
// TODO Auto-generated method stub
153+
154+
}
155+
156+
}

0 commit comments

Comments
 (0)