Snew 8 Strings
Snew 8 Strings
Programming in Java
Strings
1
Motivations
2
The String Class
• Constructing a String:
• String message = "Welcome to Java“;
• String message = new String("Welcome to
Java“);
• String s = new String();
• Obtaining String length and Retrieving Individual Characters
in a string
• String Concatenation (concat)
• Substrings (substring(index), substring(start, end))
• Comparisons (equals, compareTo)
• String Conversions
• Finding a Character or a Substring in a String
• Conversions between Strings and Arrays
• Converting Characters and Numeric Values to Strings
3
Constructing Strings
4
String Comparisons
5
String Comparisons
• equals
if (s1.equals(s2)){
System.out.println(“S1 and S2 have exact same contents”);
}
if (s1.equalsIgnoreCase(s2)) {
System.out.println(“S1 and S2 have the same contents”);
}
6
String Length, Characters, and
Combining Strings
8
Finding String Length
message = "Welcome";
message.length() (returns 7)
9
Retrieving Individual Characters in a String
• Use message.charAt(index)
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
10
Concatenation Operator
String s3 = s1.concat(s2);
String s3 = s1 + s2;
12
Extracting Substrings
13
Substrings
• Example: String greeting = “Hello, World”;
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
Examples:
String sub = greeting.substring(0, 4); “Hell”
String w = greeting.substring(7, 12); “World”
String tail = greeting.substring(7); “World!”
Extracting Substrings
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
java.lang.String
+toLowerCase(): String Returns a new string with all characters converted to lowercase.
+toUpperCase(): String Returns a new string with all characters converted to uppercase.
+trim(): String Returns a new string with blank characters trimmed on both sides.
+replace(oldChar: char, Returns a new string that replaces all matching character in this
newChar: char): String string with the new character.
+replaceFirst(oldString: String, Returns a new string that replaces the first matching substring in
newString: String): String this string with the new substring.
+replaceAll(oldString: String, Returns a new string that replace all matching substrings in this
newString: String): String string with the new substring.
+split(delimiter: String): Returns an array of strings consisting of the substrings split by the
String[] delimiter.
16
Examples
"Welcome".toLowerCase() returns a new string, welcome.
"Welcome".toUpperCase() returns a new string, WELCOME.
" Welcome ".trim() returns a new string, Welcome.
"Welcome".replace('e', 'A') returns a new string, WAlcomA.
"Welcome".replaceFirst("e", "AB") returns a new string,
WABlcome.
"Welcome".replace("e", "AB") returns a new string,
WABlcomAB.
"Welcome".replace("el", "AB") returns a new string,
WABlcome.
17
Finding a Character or a Substring in a String
java.lang.String
+indexOf(ch: char): int Returns the index of the first occurrence of ch in the string.
Returns -1 if not matched.
+indexOf(ch: char, fromIndex: Returns the index of the first occurrence of ch after fromIndex in
int): int the string. Returns -1 if not matched.
+indexOf(s: String): int Returns the index of the first occurrence of string s in this string.
Returns -1 if not matched.
+indexOf(s: String, fromIndex: Returns the index of the first occurrence of string s in this string
int): int after fromIndex. Returns -1 if not matched.
+lastIndexOf(ch: int): int Returns the index of the last occurrence of ch in the string.
Returns -1 if not matched.
+lastIndexOf(ch: int, Returns the index of the last occurrence of ch before fromIndex
fromIndex: int): int in this string. Returns -1 if not matched.
+lastIndexOf(s: String): int Returns the index of the last occurrence of string s. Returns -1 if
not matched.
+lastIndexOf(s: String, Returns the index of the last occurrence of string s before
fromIndex: int): int fromIndex. Returns -1 if not matched.
22
Finding a Character or a Substring in a
String
23
Converting Strings to char
24
Convert Character and Numbers to Strings
The String class provides several static valueOf methods for converting a
character, an array of characters, and numeric values to strings. These
methods have the same name valueOf with different argument types char,
char[], double, long, int, and float.
For example, to convert a double value to a string, use
String.valueOf(5.44).
The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’.
25