The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Manipulating Strings

The String class has several methods that appear to modify a string. Of course, strings can't be modified, so what these methods really do is create and return a second string that contains the result, as indicated in the following table.

Methods in the String Class for Manipulating Strings
Method Description
String concat(String) Concatenates the String argument to the end of this string. This is equivalent to the + operator for strings:
System.out.println("Customer: " + getNextCustomer());
If the length of the argument is 0, the original string object is returned.
String[] split(String, int)
String[] split(String)
Searches for a match as specified by the string argument (which contains a regular expression) and splits this string into an array of strings accordingly. The optional integer argument specifies the maximum size of the returned array. For more information on how to use the API for pattern matching, see Regular Expressions (in the Learning the Java Language trail).
CharSequence subSequence(int, int) Returns a new character sequence constructed from the beginning index (inclusive) up until the ending index (exclusive).
String trim() Removes white space from both ends of this string.
String toLowerCase()
String toUpperCase()
Converts this string to lower- or uppercase. If no conversions are necessary, these methods return the original string.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.