StringsNotes.
java 1/20/2016 9:37 PM
1 Lecture Notes
2 [Link]
3 Assistant Professor
4 CMR College of Engineering & Technology
5
6 Building Strings and Exploring String Class:
7 --------------------------------------------
8
9
10 The String class
11 -------------------
12 String: A String is a sequence of characters.
13
14 ex:- name,address,creditcard no,etc..,
15 In java,Any string is object of String class.
16 ->String is not a character array.
17
18 The String class has 11 constructors and more than
40 methods.
19
20 create String object:-
21 --------------------
22 [Link] can declare a String type variable and
initialize it directly with a group of characters.
23 ex:- String st = "yellaswamy";
24 [Link] can create a String object using new operator
and pass a group of characters to the object.
25 ex:- String s1 = new String("Ashok");
26 [Link] can create a character array into a string by
passing it to the String object.
27 ex:- char arr[] = {'K','s','w','a','m','y'};
28 String s2 = new String(arr);
29 ex:- String s3 = new String(arr,1,4);
30 o/p: swamy
31
32 Ex1:
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
33 class Test
34 {}{
35 public static void main(String... args)
36 {}{
37 //create a String object or constructing a String
38 //[Link] can declare a String type variable and
initialize it directly with a group of characters.
39 String str1="Yellaswamy";
40 [Link](str1);
41
42 //[Link] can create a String object using new
operator and pass a group of characters to the
object.
43 String str2=new String("Ashok");
44 [Link](str1);
45
46 //[Link] can create a character array into a string
by passing it to the String object.
47
48 char arr[] =
'K','y','e','l','l','a','s','w','a','m','y'};
{}{
49 String s1 = new String(arr);
50 [Link](s1);
51 String s2 = new String(arr,1,10);
52 [Link](s2);
53 }
54 }
55
56 output:
57
58 D:\Yellaswamy_ClassNotes\Strings>javac [Link]
59
60 D:\Yellaswamy_ClassNotes\Strings>java Test
61 Yellaswamy
62 Yellaswamy
63 Kyellaswamy
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
64 yellaswamy
65
66 D:\Yellaswamy_ClassNotes\Strings>
67 --------------------------------------------
68 Types of Obejcts:-
69 There are 2 types.
70 [Link]
71 [Link]
72 [Link]:-
73 A mutable object is an object where content can
be modified.
74 [Link]:-
75 An immutable object is an object whose content
can not be modified.
76
77 =>String objects are [Link] contents cannot
be changed.
78
79
80 String class methods:-
81 --------------------
82 String class belongs to [Link] package.
83 [Link] concat(String str):-
84 concatenates the calling string with str.
85 note:- '+' will also do the same.
86 ex:- String s1="hydera"; String s2 = "bad";
87 String s3 = [Link](s2);
88 o/p: hyderabad
89 [Link] length():-
90 Returns the length of the string.
91 String s1 = "Vijayawada";
92 int n = [Link]();
93 [Link] charAt(int i):-
94 It extracts only one character from given
[Link] character at the same ith
place.
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
95 [Link] compareTo(String str):- (case sensitive)
96 (or)
97 int compareToIgnoreCase(String str):- (case
insensitive)
98 They are used to compare two strings.
99 [Link] equals(String str):- (case sensitive)
100 (or)
101 boolean equalsIgnoreCase(String str):- (case
insensitive)
102 It returns true if the calling string equals to
str.
103 [Link] startsWith(String prefix):-
104 It returns true if the calling string starts
with prefix(begins)
105 prefix - sub string (or) not.
106 [Link] endsWith(String suffix):-
107 It returns true if the invoking string ends
with suffix.
108
109 Note:- The above two methods use case sensitive
comparison.
110 [Link] indexOf(String str):-
111 It returns the position number of substring in
the main [Link] we have to pass
substring.
112 It returns the first occurance of str in the
string.
113 EX:- "This is a book"
114 int n = [Link]("is");
115 o/p :- n = 2
116 [Link] lastIndexOf(String str):-
117 It returns last Occurance in the string.
118 [Link] replace(char oldchar,char newchar):-
119 It returns a new String that is obtained by
replacing all characters of 'oldchar' in string
with 'newchar'.
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
120
121 [Link] substring(int beginIndex):-
122 It ruturns a new String consisting of all
characters from beginindex until the end of the
String.
123
124 [Link] substring(int beginIndex,int endIndex):-
125 It returns a new String consisting all
characters from beginindex until
endindex(exclusive).
126
127 [Link] toLowerCase():-
128 It converts all characters into lowercase and
returns.
129
130 [Link] toUpperCase():-
131 IT converts all characters into uppercase and
returns.
132
133 [Link] trim():-
134 It eleminates all leading and trailing spaces.
135 ---------------------------------------------------
---
136 StringBuffer Objects are mutable.
137
138 Creating StringBuffer objects:-
139 -----------------------------
140 [Link] sb = new StringBuffer("hello");
141 [Link] sb = new StringBuffer(50);
142 [Link] sb = new StringBuffer();
143
144 [Link] methods:-
145 -------------------------------
146 1)StringBuffer append(x):-
147 x may be int,float,double,char,String (or)
[Link] will be appended to the
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
calling StringBuffer.
148 2)StringBuffer insert(int offset,x):-
149 x may be int,float,double,char,String (or)
[Link] will be inserted into the
StringBuffer at offset.
150 3)StringBuffer delete(int start,int end):-
151 Removes the characters from start to end
position.
152 4)StringBuffer reverse():-
153 It reverses the all characters in the
StringBuffer.
154 5)String toString():-
155 Converts the StringBuffer into the String.
156 purpose:- converts StringBuffer to string
class.
157 6)int length():-
158 returns the length of the StringBuffer.
159 7)int indexOf(String str):-
160 It returns the first position of subString
'str' in the StringBuffer object.
161 8)int lastIndexOf(String str):-
162 It returns the last Occurance of substring
'str' in the StringBuffer object.
163
164
165 StringBuilder:-
166 --------------
167 This class has been added in jdk1.5.0 which has
same features like StringBuffer [Link]
objects are also mutable as are the
StringBuffer objects.
168
169
170 IIQ)What is the difference between the StringBuffer
and StringBuilder classes?
171 r) StringBuffer class is synchronized and
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
StringBuilder is [Link] the programmer wants
to use several threads,he should use
StringBuffer as it gives reliable [Link] only
one thread is used,StringBuilder is prefered,as
it improves execution time.
172
173 H.W:
174 1)sort a given group of strings into alphabetical
order.
175 2)find the position of substring in a given main
string.
176 3)Test whether a given string is palindrome or not.
177
178
179
180 Ex2:
181
182
183
184 D:\Yellaswamy_ClassNotes>javap [Link]
185 Compiled from "[Link]"
186 public final class [Link] extends
[Link] implements [Link].
187 Serializable,[Link],[Link]
uence{ {
188 public static final [Link]
CASE_INSENSITIVE_ORDER;
189 public [Link]();
190 public [Link]([Link]);
191 public [Link](char[]);
192 public [Link](char[], int, int);
193 public [Link](int[], int, int);
194 public [Link](byte[], int, int, int);
195 public [Link](byte[], int);
196 public [Link](byte[], int, int,
[Link]) throws jav
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
197 [Link];
198 public [Link](byte[], int, int,
[Link]);
199 public [Link](byte[],
[Link]) throws [Link]
200 portedEncodingException;
201 public [Link](byte[],
[Link]);
202 public [Link](byte[], int, int);
203 public [Link](byte[]);
204 public
[Link]([Link]);
205 public
[Link]([Link]);
206 [Link](int, int, char[]);
207 public int length();
208 public boolean isEmpty();
209 public char charAt(int);
210 public int codePointAt(int);
211 public int codePointBefore(int);
212 public int codePointCount(int, int);
213 public int offsetByCodePoints(int, int);
214 void getChars(char[], int);
215 public void getChars(int, int, char[], int);
216 public void getBytes(int, int, byte[], int);
217 public byte[] getBytes([Link])
throws [Link]
218 odingException;
219 public byte[]
getBytes([Link]);
220 public byte[] getBytes();
221 public boolean equals([Link]);
222 public boolean
contentEquals([Link]);
223 public boolean
contentEquals([Link]);
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
224 public boolean
equalsIgnoreCase([Link]);
225 public int compareTo([Link]);
226 public int
compareToIgnoreCase([Link]);
227 public boolean regionMatches(int,
[Link], int, int);
228 public boolean regionMatches(boolean, int,
[Link], int, int);
229 public boolean startsWith([Link],
int);
230 public boolean startsWith([Link]);
231 public boolean endsWith([Link]);
232 public int hashCode();
233 public int indexOf(int);
234 public int indexOf(int, int);
235 public int lastIndexOf(int);
236 public int lastIndexOf(int, int);
237 public int indexOf([Link]);
238 public int indexOf([Link], int);
239 static int indexOf(char[], int, int, char[],
int, int, int);
240 public int lastIndexOf([Link]);
241 public int lastIndexOf([Link], int);
242 static int lastIndexOf(char[], int, int,
char[], int, int, int);
243 public [Link] substring(int);
244 public [Link] substring(int, int);
245 public [Link] subSequence(int,
int);
246 public [Link]
concat([Link]);
247 public [Link] replace(char, char);
248 public boolean matches([Link]);
249 public boolean
contains([Link]);
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
250 public [Link]
replaceFirst([Link],
[Link]);
251 public [Link]
replaceAll([Link], [Link]);
252 public [Link]
replace([Link],
[Link]
253 ce);
254 public [Link][]
split([Link], int);
255 public [Link][]
split([Link]);
256 public [Link]
toLowerCase([Link]);
257 public [Link] toLowerCase();
258 public [Link]
toUpperCase([Link]);
259 public [Link] toUpperCase();
260 public [Link] trim();
261 public [Link] toString();
262 public char[] toCharArray();
263 public static [Link]
format([Link], [Link][]);
264
265 public static [Link]
format([Link], [Link], ja
266 [Link][]);
267 public static [Link]
valueOf([Link]);
268 public static [Link] valueOf(char[]);
269 public static [Link] valueOf(char[],
int, int);
270 public static [Link]
copyValueOf(char[], int, int);
271 public static [Link]
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
copyValueOf(char[]);
272 public static [Link]
valueOf(boolean);
273 public static [Link] valueOf(char);
274 public static [Link] valueOf(int);
275 public static [Link] valueOf(long);
276 public static [Link] valueOf(float);
277 public static [Link] valueOf(double);
278 public native [Link] intern();
279 public int compareTo([Link]);
280 static {};};
281 }
282
283
284 -----------------------------------------
285 Programs:
286 1.
287 import [Link].*;
288 public class str1
289 {}{
290 public static void main(String args[])
291 {
292 String st1=new String();
293 st1="Java";
294 byte b[]={65,66,67,68,69,70};
295 String st2=new String(b);
296 String st3=new String(b,1,3);
297 [Link](st1);
298 [Link](st2);
299 [Link](st3);
300 }
301 }
302
303
304 output:
305 D:\Yellaswamy_ClassNotes\Strings>java str1
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
306 Java
307 ABCDEF
308 BCD
309 -----------------
310 Ex2:
311 import [Link].*;
312 public class str2
313 {}{
314 public static void main(String args[])
315 {
316 char ch[]={'a','b','c','d','e','f'};
317 String st1=new String("Java");
318 StringBuffer sb=new StringBuffer("cmrcet");
319 String st2=new String(ch);
320 String st3=new String(ch,2,4);
321 String st4=new String(sb);
322 [Link](st1);
323 [Link](st2);
324 [Link](st3);
325 [Link](st4);
326 }
327 }
328 output:
329
330 D:\Yellaswamy_ClassNotes\Strings>java str2
331 Java
332 abcdef
333 cdef
334 cmrcet
335
336 ------------------------------
337 Ex3:
338 import [Link].*;
339 public class str3
340 {}{
341 public static void main(String args[])
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
342 {
343 String s=new String("welcome");
344 int i;
345 char ch;
346 i=[Link]();
347 ch=[Link](5);
348 [Link]("Length of given string
: "+i);
349 [Link]("The character at given
index : " +ch);
350 }
351 }
352
353 output:
354
355 D:\Yellaswamy_ClassNotes\Strings>java str3
356 Length of given string : 7
357 The character at given index : m
358
359 ---------------------------------------------------
----
360 Ex4:
361 import [Link].*;
362 public class str4
363 {}{
364 public static void main(String args[])
365 {
366 String s1=new String("java");
367 String s2=new String("JAVA");
368 String s3=new String("welcome");
369 int n;
370
371 n=[Link](s3);
372 if(n==0)
373 [Link]("s1 and s3 are
equal");
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
374 else if(n>0)
375 [Link]("s1 is greater
than s3");
376 else
377 [Link]("s1 is less than
s3");
378
379 n=[Link](s2);
380 if(n==0)
381 [Link]("s1 and s2 are
equal");
382 else if(n>0)
383 [Link]("s1 is greater
than s2");
384 else
385 [Link]("s1 is less than
s2");
386
387 n=[Link](s2);
388 if(n==0)
389 [Link]("s1 and s2 are
equal");
390 else if(n>0)
391 [Link]("s1 is greater
than s2");
392 else
393 [Link]("s1 is less than
s2");
394 }
395 }
396 output:
397
398 D:\Yellaswamy_ClassNotes\Strings>java str4
399 s1 is less than s3
400 s1 is greater than s2
401 s1 and s2 are equal
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
402
403 ------------------------------------
404 Ex:5
405 import [Link].*;
406 class str5
407 {}{
408 public static void main(String args[])
409 {
410 boolean b;
411 StringBuffer sb=new
StringBuffer("welcome");
412 String s1=new String("welcome");
413 String s2=new String(" to cmrcet");
414 String s3=new String();
415 s3=[Link](s2);
416 [Link]("Concatenated String :
"+s3);
417 b=[Link](sb);
418 if(b==true)
419 [Link]("Given 2 strings
are equal");
420 else
421 [Link]("Given 2 strings
are not equal");
422 }
423 }
424 output:
425
426 D:\Yellaswamy_ClassNotes\Strings>java str5
427 Concatenated String : welcome to cmrcet
428 Given 2 strings are equal
429 ------------------------------------
430 Ex:6
431 import [Link].*;
432 class str6
433 {}{
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
434 public static void main(String args[])
435 {
436 char ch[]={'a','b','c','d','e','f'};
437 String s1=[Link](ch);
438 String s2=[Link](ch,1,4);
439 [Link](s1);
440 [Link](s2);
441 }
442 }
443 output:
444
445 D:\Yellaswamy_ClassNotes\Strings>java str6
446 abcdef
447 bcde
448
449 -----------------------------------------
450 Ex:7
451 import [Link].*;
452 public class str7
453 {}{
454 public static void main(String args[])
455 {
456 String s1=new String("WELCOME");
457 String s2=new String("welcome");
458 boolean b;
459
460 b=[Link]("come");
461 if(b==true)
462 [Link]("true");
463 else
464 [Link]("false");
465
466 b=[Link](s2);
467 if(b==true)
468 [Link]("Given 2 strings are
equal");
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
469 else
470 [Link]("Given 2 strings are
not equal");
471
472 b=[Link](s2);
473 if(b==true)
474 [Link]("Given 2 strings are
equal");
475 else
476 [Link]("Given 2 strings are
not equal");
477 }
478 }
479 output:
480
481 D:\Yellaswamy_ClassNotes\Strings>java str7
482 false
483 Given 2 strings are not equal
484 Given 2 strings are equal
485
486 --------------------------------------------------
487 Ex:8
488 import [Link].*;
489 public class str8
490 {}{
491 public static void main(String args[])
492 {
493 int i;
494 String st=new String("WELCOME");
495 byte bt[]=new byte[10];
496 char ch[]=new char[10];
497 bt=[Link]();
498 [Link]("Byte Array");
499 for(i=0;i<[Link];i++)
500 {
501 [Link](bt[i]+" ");
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
502 }
503 [Link](3,7,ch,2);
504 [Link]("Character Array");
505 for(i=0;i<[Link];i++)
506 {
507 [Link](ch[i]+" ");
508 }
509 }
510 }
511 output:
512
513 D:\Yellaswamy_ClassNotes\Strings>java str8
514 Byte Array
515 87
516 69
517 76
518 67
519 79
520 77
521 69
522 Character Array
523
524
525 C
526 O
527 M
528 E
529
530 ---------------------------------------
531 Ex:9
532 import [Link].*;
533 public class str9
534 {}{
535 public static void main(String args[])
536 {
537 String s1=new String("welcome");
[Link]|Assistant Professor|CMRCET
[Link] 1/20/2016 9:37 PM
538 String s2=new String("abcdabcdabc");
539 int id,hc;
540 hc=[Link]();
541 [Link]("Hash Code of given
String : "+hc);
542 id=[Link]('e');
543 [Link]("Index : " +id);
544 id=[Link]('e',3);
545 [Link]("Index : " +id);
546 id=[Link]("come");
547 [Link]("Index : " +id);
548 id=[Link]("ab",5);
549 [Link]("Index : " +id);
550 }
551 }
552 output:
553
554 D:\Yellaswamy_ClassNotes\Strings>java str9
555 Hash Code of given String : 1233099618
556 Index : 1
557 Index : 6
558 Index : 3
559 Index : 8
560
561 ------------------------------------------------
562
563
564
[Link]|Assistant Professor|CMRCET
���������������������������������������������������������������������������
���������������������������������������������������������������������������������
�����������������������������������������������������