Chapter 9 Strings 9.2 The String Class: 9.2.5 Obtaining Substrings 9.2.6 Converting, Replacing, and Splitting Strings
Chapter 9 Strings 9.2 The String Class: 9.2.5 Obtaining Substrings 9.2.6 Converting, Replacing, and Splitting Strings
Chapter 9 Strings 9.2 The String Class: 9.2.5 Obtaining Substrings 9.2.6 Converting, Replacing, and Splitting Strings
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 1
9.2.5 Obtaining Substrings
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 2
Obtaining Substrings
(Continued)
String s1 = "Welcome to Java";
To extract the substring from index 11 till the end we write:
String s2 = s1.substring(11);
Now S2 will contain the string “Java”.
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 3
Obtaining Substrings
(Continued)
String s1 = "Welcome to Java";
To extract the substring from index 0 till index 10 we write:
String s2 = s1.substring(0,11);
Now S2 will contain the string “Welcome to ” including a space at
the end. Note that the character at index 11 is not part of the
substring.
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 5
Examples
"Welcome".toLowerCase() returns a new string, welcome.
String s = "Java#HTML#Perl";
String[] tokens = s.split("#");
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i] + " ");
String s = "cdabefabgh";
String[] tokens = s.split("ab");
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i]+ " ");
String a = "a.jpg";
String [ ] str = a.split(".");
0 a
str
1 jpg
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 10
Converting Strings into numbers
To convert a String into an integer we use the method
Integer.parseInt() and into a double we use the method
Double.parseDouble().
Write a test program (main method) that prompts the user to enter a
string followed by a character and displays the number of occurrences
of the character in the string.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 12
Solution of Programming Exercise 9.4
import java.util.Scanner;
public class Exercise09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = input.next();
System.out.print("Enter a character: ");
char ch = input.next().charAt(0);
int counter = count(s, ch);
System.out.println(counter);
}
public static int count(String str, char ch) {
int counter = 0;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == ch)
counter++;
return counter;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 13