Skip to main content
ICT
Lesson A10 - The String Class
 
Main   Previous Next
 

F. String Translation Methods page 8 of 17

Translate Method
Sample Syntax
String toLowerCase(); String greeting = "Hi World!";
greeting = greeting.toLowerCase();
// greeting <- "hi world!"
String toUpperCase(); String greeting = "Hi World!";
greeting = greeting.toUpperCase();
// greeting <- "HI WORLD!"
String trim(); String needsTrim = " trim me! ";
needsTrim = needsTrim.trim();
// needsTrim <- "trim me!"
String substring(int beginIndex) String sample = "hamburger";
sample = sample.substring(3);
// sample <- "burger"
String substring(int beginIndex, int endIndex) String sample = "hamburger";
sample = sample.substring(4, 8);
// sample <- "urge"
  1. toLowerCase() returns a String with the same characters as the String object, but with all characters converted to lowercase. Notice that in all of the above samples, the String object is placed on the left hand side of the assignment statement. This is necessary because Strings in Java are immutable. Please see section G for a full explanation of immutable.

  2. toUpperCase() returns a String with the same characters as the String object, but with all characters converted to uppercase.

  3. trim() returns a String with the same characters as the String object, but with the leading and trailing whitespace removed.

  4. substring(int beginIndex) returns the substring of the String object starting from beginIndex through to the end of the String object.

  5. substring(int beginIndex, int endIndex) returns the substring of the String object starting from beginIndex through, but not including, position endIndex of the String object. That is, the new String contains characters numbered beginIndex to endIndex-1 in the original String.

 

Main   Previous Next
Contact
 © ICT 2006, All Rights Reserved.