Week 4 - Lecture 2 - Chapter 9
Week 4 - Lecture 2 - Chapter 9
CHAPTER 9 STRINGS
• Constructing a String:
Example:
String s = new String("Welcome to Java");
Java treats a string literal as a String object. Thus, the
following statement is also valid:
String s = "Welcome to Java";
STRINGS ARE IMMUTABLE
3
String s = "Java";
s = "HTML";
String s = "Java";
s = "HTML";
java.lang.String
+equals(s1: String): boolean Returns true if this string is equal to string s1.
+equalsIgnoreCase(s1: String): Returns true if this string is equal to string s1 case-
boolean insensitive.
+compareTo(s1: String): int Returns an integer greater than 0, equal to 0, or less than 0
to indicate whether this string is greater than, equal to, or
less than s1.
+compareToIgnoreCase(s1: String): Same as compareTo except that the comparison is case-
int insensitive.
+regionMatches(index: int, s1: String, Returns true if the specified subregion of this string exactly
s1index: int, len: int): boolean matches the specified subregion in string s1.
+startsWith(prefix: String): boolean Returns true if this string starts with the specified prefix.
+endsWith(suffix: String): boolean Returns true if this string ends with the specified suffix.
STRING COMPARISONS (CONTINUED)
7
if (s1.equals(s2))
// checks if s1 and s2 have the same contents
// For the above example it gives true
if (s1.equals(s3))
// checks if s1 and s3 have the same contents
// For the above example it gives false
STRING COMPARISONS (CONTINUED)
8
if (s1 == s2)
// checks if s1 and s2 have the same reference
// For the above example it gives false
if (s1 == s3) {
// checks if s1 and s3 have the same reference
// For the above example it gives true
STRING COMPARISONS (CONTINUED)
9
• equalsIgnoreCase()
Ignores the letter case (lower versus upper case) when comparing
if (s1.equals(s2))
// gives false since w is different from W
if (s1.equalsIgnoreCase(s2))
// gives true since w and W are considered the same
STRING COMPARISONS (CONTINUED)
10
• compareTo()
s1.compareTo(s2)
This method returns 0 if s1 is equal to s2, a value less than 0 if s1 is
lexicographically (i.e., in terms of Unicode ordering) less than s2,
and a value greater than 0 if s1 is lexicographically greater than s2.
String s1 = new String("Welcome");
String s2 = "Welcome";
if (s1.compareTo(s2) > 0) // is s1 greater than s2?
• compareTo()
x = s1.compareTo(s3);
// x > 0 since s1 > s3 because ‘c’ is > ‘a’ (‘c’ = 99 & ‘a’ = 97)
// See ASCII Table
x = s1.compareTo(s4);
// x < 0 since s1 < s4 because ‘c’ is < ‘d’ (‘c’ = 99 & ‘d’ = 100)
STRING COMPARISONS (CONTINUED)
12
• compareToIgnoreCase()
x = s1.compareTo(s2);
// x > 0 since s1 > s2 because ‘a’ > ‘A’ (‘a’ = 97, ‘A’ = 65)
x = s1.compareToIgnoreCase(s2);
// x = 0 since s1 and s2 are equal when the case is ignored
STRING COMPARISONS (CONTINUED)
13
• regionMatches()
if (s1.regionMatches(1,s2,2,3))
// 1 is the starting index in s1, and 2 is the starting index in s2, 3
is the length or the number of characters to be compared.
STRING COMPARISONS (CONTINUED)
14
String s = "howtodoinjava.com";
java.lang.String
+length(): int Returns the number of characters in this string.
+charAt(index: int): char Returns the character at the specified index from this string.
+concat(s1: String): String Returns a new string that concatenate this string with string s1.
string.
FINDING STRING LENGTH
16
String s = "Welcome";
int len = s.length(); // len = 7
RETRIEVING A CHAR IN A STRING
17
Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
message W e l c o m e t o J a v a
Output
W
e
l
c
o
m
e
STRING CONCATENATION
19
java.lang.String
+substring(beginIndex: int): Returns this string’s substring that begins with the character at the
String specified beginIndex and extends to the end of the string.
+substring(beginIndex: int, Returns this string’s substring that begins at the specified
endIndex: int): String beginIndex and extends to the character at index endIndex – 1.
Note that the character at endIndex is not part of the substring.
You can extract a single character from a string using the charAt
method. You can also extract a substring from a string using the
substring method in the String class.
OBTAINING SUBSTRINGS (CONTINUED)
21
Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
message W e l c o m e t o J a v a
Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
message W e l c o m e t o J a v a
The split method can be used to extract tokens from a string with the specified delimiters.
For example, in the following code the delimiter is the string “#”.
String s = "Java#HTML#Perl";
String[] tokens = s.split("#");
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i] + " ");
The delimiter # is not stored in the array tokens (an array of
strings).
0 Java
The output will be:
tokens 1 HTML Java HTML Perl
2 Perl
SPLITTING A STRING – EXAMPLE 2
26
The delimiter can be composed of more than one character since it is a string. For example,
in the following code the delimiter is “ab”.
String s = "cdabefabgh";
String[] tokens = s.split("ab");
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i]+ " ");
0 a
str
1 jpg