Chapter 9 Strings 9.2 The String Class: 9.2.5 Obtaining Substrings 9.2.6 Converting, Replacing, and Splitting Strings

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

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

message.substring(0, 11) message.substring(11)

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

message.substring(0, 11) message.substring(11)


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 4
9.2.6 Converting, Replacing, and
Splitting Strings
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, newChar: Returns a new string that replaces all matching character in this string
char): String with the new character.
+replaceFirst(oldString: String, Returns a new string that replaces the first matching substring in this
newString: String): String string with the new substring.
+replaceAll(oldString: String, Returns a new string that replace all matching substrings in this string
newString: String): String with the new substring.
+replace(oldString: String, Works exactly like replaceAll.
newString: String): String
Returns an array of strings consisting of the substrings split by the
+split(delimiter: String): String[] delimiter.

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.

"Welcome".toUpperCase() returns a new string, WELCOME.

" Welcome to Java ".trim() returns a new string, Welcome to Java.

"Welcome".replace('e', 'A') returns a new string, WAlcomA.

"Welcome".replaceFirst("e", "AB") returns a new string, WABlcome.

"Welcome".replaceAll("e", "AB") returns a new string,


WABlcomAB.

"Welcome".replace("e", "AB") returns a new string, WABlcomAB.


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 6
Splitting a String – Example 1
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


0 Java (an array of strings).
tokens 1 HTML The output will be:
2 Perl
Java HTML Perl
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 7
Splitting a String – Example 2
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 cd The output will be:


tokens 1 ef cd ef gh
2 gh
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 8
Splitting a String – Example 3
The delimiter can be a space.

String s = "Welcome to Java";


String[] tokens = s.split(" ");
for (int i = 0; i < tokens.length; i++)
System.out.println(tokens[i]);

The output will be:


Welcome
0 Welcome
to
tokens 1 to
2 Java
Java
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 9
The “.” delimiter
If want to split a string using the delimiter dot “.” such as

String a = "a.jpg";
String [ ] str = a.split(".");

The split does not work because "." is a reserved character.


Instead, we should use the following statement:

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().

String s1 = "12", s2 = "12.5";


int n = Integer.parseInt(s1);
double m = Double.parseDouble(s2);

As a result n will contain the integer value 12 and m will


contain the double value 12.5.
Note that if s1 or s2 contain non digit characters then an error
will occur. Of course s2 can contain the dot.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807 11
Programming Exercise 9.4 in your
textbook page 363: Occurrence of a
specified character in a string
Write a method that finds the number of occurrences of a specified
character in a string using the following header:
public static int count(String str, char a)
For example, count("Welcome", 'e') returns 2.

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

You might also like