Stringsnotes PDF
Stringsnotes PDF
Stringsnotes PDF
1 Lecture Notes
2 K.Yellaswamy
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 1.We can declare a String type variable and
initialize it directly with a group of characters.
23 ex:- String st = "yellaswamy";
24 2.We 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 3.We 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:
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 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 //1.We can declare a String type variable and
initialize it directly with a group of characters.
39 String str1="Yellaswamy";
40 System.out.println(str1);
41
42 //2.We can create a String object using new
operator and pass a group of characters to the
object.
43 String str2=new String("Ashok");
44 System.out.println(str1);
45
46 //3.We 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 System.out.println(s1);
51 String s2 = new String(arr,1,10);
52 System.out.println(s2);
53 }
54 }
55
56 output:
57
58 D:\Yellaswamy_ClassNotes\Strings>javac Test.java
59
60 D:\Yellaswamy_ClassNotes\Strings>java Test
61 Yellaswamy
62 Yellaswamy
63 Kyellaswamy
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 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 1.Mutable
71 2.Immutable
72 1.Mutable:-
73 A mutable object is an object where content can
be modified.
74 2.Immutable:-
75 An immutable object is an object whose content
can not be modified.
76
77 =>String objects are immutable.its contents cannot
be changed.
78
79
80 String class methods:-
81 --------------------
82 String class belongs to java.lang package.
83 1.String 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 = s1.concat(s2);
88 o/p: hyderabad
89 2.int length():-
90 Returns the length of the string.
91 String s1 = "Vijayawada";
92 int n = s1.length();
93 3.char charAt(int i):-
94 It extracts only one character from given
String.That character at the same ith
place.
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
120
121 11.String substring(int beginIndex):-
122 It ruturns a new String consisting of all
characters from beginindex until the end of the
String.
123
124 12.String substring(int beginIndex,int endIndex):-
125 It returns a new String consisting all
characters from beginindex until
endindex(exclusive).
126
127 13.String toLowerCase():-
128 It converts all characters into lowercase and
returns.
129
130 14.String toUpperCase():-
131 IT converts all characters into uppercase and
returns.
132
133 15.String trim():-
134 It eleminates all leading and trailing spaces.
135 ---------------------------------------------------
---
136 StringBuffer Objects are mutable.
137
138 Creating StringBuffer objects:-
139 -----------------------------
140 1.StringBuffer sb = new StringBuffer("hello");
141 2.StringBuffer sb = new StringBuffer(50);
142 3.StringBuffer sb = new StringBuffer();
143
144 java.lang.StringBuffer methods:-
145 -------------------------------
146 1)StringBuffer append(x):-
147 x may be int,float,double,char,String (or)
StringBuffer.It will be appended to the
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 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)
StringBuffer.It 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 class.These
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
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
197 a.io.UnsupportedEncodingException;
198 public java.lang.String(byte[], int, int,
java.nio.charset.Charset);
199 public java.lang.String(byte[],
java.lang.String) throws java.io.Unsup
200 portedEncodingException;
201 public java.lang.String(byte[],
java.nio.charset.Charset);
202 public java.lang.String(byte[], int, int);
203 public java.lang.String(byte[]);
204 public
java.lang.String(java.lang.StringBuffer);
205 public
java.lang.String(java.lang.StringBuilder);
206 java.lang.String(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(java.lang.String)
throws java.io.UnsupportedEnc
218 odingException;
219 public byte[]
getBytes(java.nio.charset.Charset);
220 public byte[] getBytes();
221 public boolean equals(java.lang.Object);
222 public boolean
contentEquals(java.lang.StringBuffer);
223 public boolean
contentEquals(java.lang.CharSequence);
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
copyValueOf(char[]);
272 public static java.lang.String
valueOf(boolean);
273 public static java.lang.String valueOf(char);
274 public static java.lang.String valueOf(int);
275 public static java.lang.String valueOf(long);
276 public static java.lang.String valueOf(float);
277 public static java.lang.String valueOf(double);
278 public native java.lang.String intern();
279 public int compareTo(java.lang.Object);
280 static {};};
281 }
282
283
284 -----------------------------------------
285 Programs:
286 1.
287 import java.io.*;
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 System.out.println(st1);
298 System.out.println(st2);
299 System.out.println(st3);
300 }
301 }
302
303
304 output:
305 D:\Yellaswamy_ClassNotes\Strings>java str1
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
306 Java
307 ABCDEF
308 BCD
309 -----------------
310 Ex2:
311 import java.io.*;
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 System.out.println(st1);
323 System.out.println(st2);
324 System.out.println(st3);
325 System.out.println(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 java.io.*;
339 public class str3
340 {}{
341 public static void main(String args[])
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
342 {
343 String s=new String("welcome");
344 int i;
345 char ch;
346 i=s.length();
347 ch=s.charAt(5);
348 System.out.println("Length of given string
: "+i);
349 System.out.println("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 java.io.*;
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=s1.compareTo(s3);
372 if(n==0)
373 System.out.println("s1 and s3 are
equal");
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
402
403 ------------------------------------
404 Ex:5
405 import java.io.*;
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=s1.concat(s2);
416 System.out.println("Concatenated String :
"+s3);
417 b=s1.contentEquals(sb);
418 if(b==true)
419 System.out.println("Given 2 strings
are equal");
420 else
421 System.out.println("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 java.io.*;
432 class str6
433 {}{
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
469 else
470 System.out.println("Given 2 strings are
not equal");
471
472 b=s1.equalsIgnoreCase(s2);
473 if(b==true)
474 System.out.println("Given 2 strings are
equal");
475 else
476 System.out.println("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 java.io.*;
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=st.getBytes();
498 System.out.println("Byte Array");
499 for(i=0;i<bt.length;i++)
500 {
501 System.out.println(bt[i]+" ");
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
502 }
503 st.getChars(3,7,ch,2);
504 System.out.println("Character Array");
505 for(i=0;i<ch.length;i++)
506 {
507 System.out.println(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 java.io.*;
533 public class str9
534 {}{
535 public static void main(String args[])
536 {
537 String s1=new String("welcome");
K.Yellaswamy|Assistant Professor|CMRCET
StringsNotes.java 1/20/2016 9:37 PM
K.Yellaswamy|Assistant Professor|CMRCET
���������������������������������������������������������������������������
���������������������������������������������������������������������������������
�����������������������������������������������������