s.toLowerCase( )
returns:
a new string where all upper case letters in s are
replaced by lower case letters
Note:
the original string s is unchanged
|
Examples:
"AbC".toLowerCase( ) returns "abc" "A&C".toLowerCase( ) returns "a&c" |
s.toUpperCase( )
returns:
a new string where all lower case letters in s are
replaced by upper case letters
Note:
the original string s is unchanged
|
Examples:
"AbC".toLowerCase( ) returns "ABC" "Ab%".toLowerCase( ) returns "AB%" |
Java program that illustrates toLowerCase() and toUpperCase():
public class Case1 { public static void main( String[] args ) { String s, r; s = "AbC"; r = s.toLowerCase( ); System.out.println( "\"" + s + "\"" + ".toLowerCase( ) = " + "\"" + r + "\"" ); System.out.println( "Afterwards: s = " + "\"" + s + "\""); s = "Ab%"; r = s.toLowerCase( ); System.out.println( "\"" + s + "\"" + ".toLowerCase( ) = " + "\"" + r + "\"" ); System.out.println( "Afterwards: s = " + "\"" + s + "\"\n\n"); /* ================================================== */ s = "AbC"; r = s.toUpperCase( ); System.out.println( "\"" + s + "\"" + ".toUpperCase( ) = " + "\"" + r + "\"" ); System.out.println( "Afterwards: s = " + "\"" + s + "\""); s = "Ab%"; r = s.toUpperCase( ); System.out.println( "\"" + s + "\"" + ".toUpperCase( ) = " + "\"" + r + "\"" ); System.out.println( "Afterwards: s = " + "\"" + s + "\""); } } |
Output:
"AbC".toLowerCase( ) = "abc" Afterwards: s = "AbC" *** s is UNCHANGED ! "Ab%".toLowerCase( ) = "ab%" Afterwards: s = "Ab%" *** s is UNCHANGED ! "AbC".toUpperCase( ) = "ABC" Afterwards: s = "AbC" *** s is UNCHANGED ! "Ab%".toUpperCase( ) = "AB%" Afterwards: s = "Ab%" *** s is UNCHANGED ! |
How to run the program:
|
s.replaceFirst( old, new )
returns:
a new string where the first occurence of "old" in s is
replaced by "new"
Note:
the original string s is unchanged
|
Examples:
"Hello World, Bye World".replaceFirst( "World", "Class" ) returns "Hello Class, Bye World" |
s.replaceAll( old, new )
returns:
a new string where all occurence of "old" in s is
replaced by "new"
Note:
the original string s is unchanged
|
Examples:
"Hello World, Bye World".replaceAll( "World", "Class" ) returns "Hello Class, Bye Class" |
Java program that illustrates replaceFirst() and replaceAll():
public class Replace1 { public static void main( String[] args ) { String s, r; s = "Hello World, Bye World"; r = s.replaceFirst( "World", "Class" ); System.out.println( "\"" + s + "\"" + ".replaceFirst( \"World\", \"Class\" ) = " + "\"" + r + "\"" ); System.out.println( "Afterwards: s = " + "\"" + s + "\"\n\n" ); /* ================================================== */ r = s.replaceAll( "World", "Class" ); System.out.println( "\"" + s + "\"" + ".replaceFirst( \"World\", \"Class\" ) = " + "\"" + r + "\"" ); System.out.println( "Afterwards: s = " + "\"" + s + "\"" ); } } |
Output:
"Hello World, Bye World".replaceFirst( "World", "Class" ) = "Hello Class, Bye World" Afterwards: s = "Hello World, Bye World" "Hello World, Bye World".replaceAll( "World", "Class" ) = "Hello Class, Bye Class" Afterwards: s = "Hello World, Bye World" |
How to run the program:
|
|
|