string class in java
DESCRIPTION
String Class in Java. java.lang Class String java.lang.Object java.lang.String We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;. Two ways to create a String object. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/1.jpg)
String Class in Java
• java.lang Class String
• java.lang.Object java.lang.String
• We do not have to import the String class since it comes from java.lang.
• An object of the String class represents a string of characters. “abcde”;
![Page 2: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/2.jpg)
Two ways to create a String object
• Create a String literal:
String str = "abc";
• With the key word new since it is an object
String str = new String(“abc”);
![Page 3: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/3.jpg)
10-3
Empty Strings• An empty string has no characters;
• Its contents are “null”.
String s1 = “"; String s2 = new String();
Empty strings
![Page 4: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/4.jpg)
String indexes
• String is a sequence of characters.
Each letter in the string has its own index location and can be accessed by it.
index location
10-4
0 1 2 3 4 5 6 7
c o m p u t e r
The length of the String is how many letters it contains: 8The last index of the String is length – 1; 7
![Page 5: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/5.jpg)
10-5
Index locations Index locations are from 0 to length-1
String s = “strawberry”;
strawberry // 10 letters in the word
// index from 0 to 9
int len = s.length() // 10
int lastIndex = s.length()-1 //9
S is the String object I created. Objects call methods with a dot operator.
![Page 6: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/6.jpg)
10-6
String Methods: • Page 78
• There are many ways to manipulate Strings.
• Look on page 78 those tested on AP
• You always use the period to separate the object from the method.
• int len = s1.length();
![Page 7: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/7.jpg)
10-7
Methods — length()
int length (); returns an int
• Returns the number of characters in the string
6
4
int lenF = f.length();
int lenW = w.length();
Returns:
String f = “Flower”;
String w = “Wind”;
![Page 8: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/8.jpg)
10-8
Methods — substring
String s2 = s.substring (i, k);
returns the substring of chars in positions from i to k-1
String s3 = s.substring (i);
returns the substring from i char to the end
String s = “strawberry”;
rawrawberry
String s2 = s.substring (2,5); start at 2 end at 4 String s3 = s.substring (2); start at 2 thru end
Returns:
strawberry
i k
strawberry
i
Strings have index locations from 0 to length-1
![Page 9: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/9.jpg)
String n = “computer";
String one = n.substring(0,7);
String two = n.substring(1,6);
String three = n.substring(2,5);
String four = n.substring(4);
String five = n.substring(3);
String six = n.substring(1,n.length()-2);
String seven = six.substring(0, n.length()/2);
10-9
0 1 2 3 4 5 6 7
c o m p u t e r
s.substring(i, k);returns the substring of chars in positions from i to k-1
s.substring(i);returns the substring from i char to the end
![Page 10: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/10.jpg)
10-10
Methods — Concatenation
String s1 = “obi”;String s2 = “wan”;
String result = s1 + s2;obiwan
String result = s1.concat (s2);the same as s1 + s2 obiwan
![Page 11: String Class in Java](https://reader036.vdocument.in/reader036/viewer/2022082414/5681454c550346895db21cb2/html5/thumbnails/11.jpg)
10-11
Methods — Find (indexOf)
String date ="July 5, 2012 1:28:19 PM";
date.indexOf ('J'); 0
date.indexOf ('2'); 8
date.indexOf ("2012"); 8
date.indexOf ('2', 9); 11
date.indexOf ("2020"); -1
date.lastIndexOf ('2'); 15
Returns:
(not found)
(starts searching at position 9)
0 8 11 15
Index of return the index location of the first occurrence of the character requested.