Learning Objectives
After completing this session, you will be able to:
Write Java programs using String class
The String Class: Example
1 class StringDemo {
2 public static void main(String args[]) {
3 String name = "Jonathan";
4 System.out.println("name: " + name);
5 System.out.println("3rd character of name: " +
6 name.charAt(2));
7 /* character that first appears alphabetically
8 has lower unicode value */
9 System.out.println("Jonathan compared to Solomon: "
10 + name.compareTo("Solomon"));
11 System.out.println("Solomon compared to Jonathan: "
12 + "Solomon".compareTo("Jonathan"));
13 /* 'J' has lower unicode value compared to 'j' */
14 System.out.println("Jonathan compared to jonathan: " +
15 name.compareTo("jonathan"));
16 System.out.println("Jonathan compared to jonathan
17 (ignore case): " + name.compareToIgnoreCase("jonathan"));
18 System.out.println("Is Jonathan equal to Jonathan? " +
name.equals("Jonathan"));
20 //continued...
21 System.out.println("Is Jonathan equal to jonathan? " +
22 name.equals("jonathan"));
23 System.out.println("Is Jonathan equal to jonathan
(ignore case)? " + name.equalsIgnoreCase("jonathan"));
25 char charArr[] = "Hi XX".toCharArray();
26 /* Need to add 1 to the endSrc index of getChars */
27 "Jonathan".getChars(0, 2, charArr, 3);
28 System.out.print("getChars method: ");
29 System.out.println(charArr);
30 System.out.println("Length of name: " +
31 name.length());
32 System.out.println("Replace a's with e's in name: " +
33 name.replace('a', 'e'));
34 /* Need to add 1 to the endIndex parameter of
35 substring*/
36 System.out.println("A substring of name: " +
37 name.substring(0, 2));
38 //continued...
39 System.out.println("Trim \" a b c d e f \": \"" +
40 " a b c d e f ".trim() + "\"");
41 System.out.println("String representation of boolean
42 expression 10>10: " + String.valueOf(10>10));
43 /* toString method is implicitly called in the println
44 method*/
45 System.out.println("String representation of boolean
46 expression 10<10: " + (10<10));
47 /* Note there's no change in the String object name
48 even after applying all these methods. */
49 System.out.println("name: " + name);
50 }
51 }
Try It Out
Problem Statement:
Write a program that illustrates the usage of various methods available in the String class.
Code:
public class StringApp {
public static void main(String args[]) {
String s = " Java 2 Certification ";
System.out.println(s);
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println("[" + s + "]");
s = s.trim();
System.out.println("[" + s + "]");
s = s.replace('J', 'X');
s = s.replace('C', 'Y');
s = s.replace('2', 'Z');
System.out.println(s);
How It Works:
This program performs several manipulations of a string s, which is initially set to "
Java 2 Certification ".
It prints the original string and then printsuppercase and lowercase versions of it,
illustrating the use of the toUpperCase() and toLowerCase() methods.
It prints the string enclosed between two braces to show that it contains leading and
trailing spaces.
It then trims away these spaces using the trim() method and reprints the string to show
that these spaces were removed.
The program uses the replace() method to replace 'J‘ , 'C‘ , and '2' with 'X', 'Y', and 'Z',
and prints out the string to show the changes.
The replace() method is case sensitive.
It uses the indexOf() method to get the indices of 'X', 'Y', and 'Z' within s.
It uses the toCharArray() to convert the string to a char array.
It then uses the indices to put 'J', 'C', and '2' back in their proper locations within the
character array.
The String() constructor is used to construct a new string from the character array.
The new string is assigned to s and is printed.
The output of the program is as follows:
Java 2 Certification
JAVA 2 CERTIFICATION
java 2 certification
[ Java 2 Certification ]
[Java 2 Certification]
Xava Z Yertification
Java 2 Certification
Tips and Tricks:
What will happen if you concatenate null with a string?
Solution:
String concatenation process will add a string with the value of “null”, if an object
reference is null and that object is appearing in a concatenation expression by itself.
But if you try to access its members or methods, then a NullPointerException is
thrown.
The same is true for arrays. Array name is replaced with null, but trying to index it
when the null of it throws a NullPointerException.
Summary
String methods to remember are charAt(), concat(), equalsIgnoreCase(), length(),
replace(), substring(), toLowerCase(),toString(), toUpperCase(), and trim().
Strings have a method: length(); arrays have an attribute named length.
When the JVM finds a String literal, it is added to the String literal pool.
Remember that chained methods are evaluated from left to right.
Test Your Understanding
Differentiate the methods compareTo(), equals(), and equalsIgnoreCase().
After completing this session, you will be able to:
Write Java programs using String class
The String Class: Example
1 class StringDemo {
2 public static void main(String args[]) {
3 String name = "Jonathan";
4 System.out.println("name: " + name);
5 System.out.println("3rd character of name: " +
6 name.charAt(2));
7 /* character that first appears alphabetically
8 has lower unicode value */
9 System.out.println("Jonathan compared to Solomon: "
10 + name.compareTo("Solomon"));
11 System.out.println("Solomon compared to Jonathan: "
12 + "Solomon".compareTo("Jonathan"));
13 /* 'J' has lower unicode value compared to 'j' */
14 System.out.println("Jonathan compared to jonathan: " +
15 name.compareTo("jonathan"));
16 System.out.println("Jonathan compared to jonathan
17 (ignore case): " + name.compareToIgnoreCase("jonathan"));
18 System.out.println("Is Jonathan equal to Jonathan? " +
name.equals("Jonathan"));
20 //continued...
21 System.out.println("Is Jonathan equal to jonathan? " +
22 name.equals("jonathan"));
23 System.out.println("Is Jonathan equal to jonathan
(ignore case)? " + name.equalsIgnoreCase("jonathan"));
25 char charArr[] = "Hi XX".toCharArray();
26 /* Need to add 1 to the endSrc index of getChars */
27 "Jonathan".getChars(0, 2, charArr, 3);
28 System.out.print("getChars method: ");
29 System.out.println(charArr);
30 System.out.println("Length of name: " +
31 name.length());
32 System.out.println("Replace a's with e's in name: " +
33 name.replace('a', 'e'));
34 /* Need to add 1 to the endIndex parameter of
35 substring*/
36 System.out.println("A substring of name: " +
37 name.substring(0, 2));
38 //continued...
39 System.out.println("Trim \" a b c d e f \": \"" +
40 " a b c d e f ".trim() + "\"");
41 System.out.println("String representation of boolean
42 expression 10>10: " + String.valueOf(10>10));
43 /* toString method is implicitly called in the println
44 method*/
45 System.out.println("String representation of boolean
46 expression 10<10: " + (10<10));
47 /* Note there's no change in the String object name
48 even after applying all these methods. */
49 System.out.println("name: " + name);
50 }
51 }
Try It Out
Problem Statement:
Write a program that illustrates the usage of various methods available in the String class.
Code:
public class StringApp {
public static void main(String args[]) {
String s = " Java 2 Certification ";
System.out.println(s);
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println("[" + s + "]");
s = s.trim();
System.out.println("[" + s + "]");
s = s.replace('J', 'X');
s = s.replace('C', 'Y');
s = s.replace('2', 'Z');
System.out.println(s);
How It Works:
This program performs several manipulations of a string s, which is initially set to "
Java 2 Certification ".
It prints the original string and then printsuppercase and lowercase versions of it,
illustrating the use of the toUpperCase() and toLowerCase() methods.
It prints the string enclosed between two braces to show that it contains leading and
trailing spaces.
It then trims away these spaces using the trim() method and reprints the string to show
that these spaces were removed.
The program uses the replace() method to replace 'J‘ , 'C‘ , and '2' with 'X', 'Y', and 'Z',
and prints out the string to show the changes.
The replace() method is case sensitive.
It uses the indexOf() method to get the indices of 'X', 'Y', and 'Z' within s.
It uses the toCharArray() to convert the string to a char array.
It then uses the indices to put 'J', 'C', and '2' back in their proper locations within the
character array.
The String() constructor is used to construct a new string from the character array.
The new string is assigned to s and is printed.
The output of the program is as follows:
Java 2 Certification
JAVA 2 CERTIFICATION
java 2 certification
[ Java 2 Certification ]
[Java 2 Certification]
Xava Z Yertification
Java 2 Certification
Tips and Tricks:
What will happen if you concatenate null with a string?
Solution:
String concatenation process will add a string with the value of “null”, if an object
reference is null and that object is appearing in a concatenation expression by itself.
But if you try to access its members or methods, then a NullPointerException is
thrown.
The same is true for arrays. Array name is replaced with null, but trying to index it
when the null of it throws a NullPointerException.
Summary
String methods to remember are charAt(), concat(), equalsIgnoreCase(), length(),
replace(), substring(), toLowerCase(),toString(), toUpperCase(), and trim().
Strings have a method: length(); arrays have an attribute named length.
When the JVM finds a String literal, it is added to the String literal pool.
Remember that chained methods are evaluated from left to right.
Test Your Understanding
Differentiate the methods compareTo(), equals(), and equalsIgnoreCase().
No comments:
Post a Comment