TheString
class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks.Getting Characters and Substrings by Index
You can get the character at a particular index within a string by invoking thecharAt()
accessor method. The index of the first character is 0, while the index of the last character islength()-1
. For example, the following code gets the character at index 9 in a string:Indices begin at 0, so the character at index 9 is 'O', as illustrated in the following figure:String anotherPalindrome = "Niagara. O roar again!"; char aChar = anotherPalindrome.charAt(9);If you want to get more than one consecutive character from a string, you can use the
substring
method. Thesubstring
method has two versions, as shown in the following table:
The substring
Methods in theString
ClassMethod Description String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The first integer argument specifies the index of the first character. The second integer argument is the index of the last character + 1. String substring(int beginIndex)
Returns a new string that is a substring of this string. The integer argument specifies the index of the first character. Here, the returned substring extends to the end of the original string. The following code gets from the Niagara palindrome the substring that extends from index 11 up to, but not including, index 15, which is the word "roar":
String anotherPalindrome = "Niagara. O roar again!"; String roar = anotherPalindrome.substring(11, 15);Other Methods for Manipulating Strings
Here are several otherString
methods for manipulating strings:
Other Methods in the String
Class for Manipulating StringsMethod Description String[] split(String regex)
String[] split(String regex, int limit)
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. Regular expressions are covered in the lesson titled "Regular Expressions." CharSequence subSequence(int beginIndex, int endIndex)
Returns a new character sequence constructed from beginIndex
index up untilendIndex
- 1.String trim()
Returns a copy of this string with leading and trailing white space removed. String toLowerCase()
String toUpperCase()Returns a copy of this string converted to lowercase or uppercase. If no conversions are necessary, these methods return the original string. Searching for Characters and Substrings in a String
Here are some otherString
methods for finding characters or substrings within a string. TheString
class provides accessor methods that return the position within the string of a specific character or substring:indexOf()
andlastIndexOf()
. TheindexOf()
methods search forward from the beginning of the string, and thelastIndexOf()
methods search backward from the end of the string. If a character or substring is not found,indexOf()
andlastIndexOf()
return -1.The
String
class also provides a search method,contains
, that returns true if the string contains a particular character sequence. Use this method when you only need to know that the string contains a character sequence, but the precise location isn't important.The following table describes the various string search methods.
The Search Methods in the String
ClassMethod Description int indexOf(int ch)
int lastIndexOf(int ch)Returns the index of the first (last) occurrence of the specified character. int indexOf(int ch, int fromIndex)
int lastIndexOf(int ch, int fromIndex)Returns the index of the first (last) occurrence of the specified character, searching forward (backward) from the specified index. int indexOf(String str)
int lastIndexOf(String str)Returns the index of the first (last) occurrence of the specified substring. int indexOf(String str, int fromIndex)
int lastIndexOf(String str, int fromIndex)Returns the index of the first (last) occurrence of the specified substring, searching forward (backward) from the specified index. boolean contains(CharSequence s)
Returns true if the string contains the specified character sequence.
Note:CharSequence
is an interface that is implemented by theString
class. Therefore, you can use a string as an argument for thecontains()
method.Replacing Characters and Substrings into a String
TheString
class has very few methods for inserting characters or substrings into a string. In general, they are not needed: You can create a new string by concatenation of substrings you have removed from a string with the substring that you want to insert.The
String
class does have four methods for replacing found characters or substrings, however. They are:
Methods in the String
Class for Manipulating StringsMethod Description String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. String replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement. String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement. An Example
The following class,Filename
, illustrates the use oflastIndexOf()
andsubstring()
to isolate different parts of a file name.
Note: The methods in the followingFilename
class don't do any error checking and assume that their argument contains a full directory path and a filename with an extension. If these methods were production code, they would verify that their arguments were properly constructed.Here is a program,public class Filename { private String fullPath; private char pathSeparator, extensionSeparator; public Filename(String str, char sep, char ext) { fullPath = str; pathSeparator = sep; extensionSeparator = ext; } public String extension() { int dot = fullPath.lastIndexOf(extensionSeparator); return fullPath.substring(dot + 1); } public String filename() { // gets filename without extension int dot = fullPath.lastIndexOf(extensionSeparator); int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(sep + 1, dot); } public String path() { int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(0, sep); } }FilenameDemo
, that constructs aFilename
object and calls all of its methods:And here's the output from the program:public class FilenameDemo { public static void main(String[] args) { final String FPATH = "/home/mem/index.html"; Filename myHomePage = new Filename(FPATH, '/', '.'); System.out.println("Extension = " + myHomePage.extension()); System.out.println("Filename = " + myHomePage.filename()); System.out.println("Path = " + myHomePage.path()); } }As shown in the following figure, ourExtension = html Filename = index Path = /home/memextension
method useslastIndexOf
to locate the last occurrence of the period (.) in the file name. Thensubstring
uses the return value oflastIndexOf
to extract the file name extension that is, the substring from the period to the end of the string. This code assumes that the file name has a period in it; if the file name does not have a period,lastIndexOf
returns -1, and the substring method throws aStringIndexOutOfBoundsException
.Also, notice that the
extension
method usesdot + 1
as the argument tosubstring
. If the period character (.) is the last character of the string,dot + 1
is equal to the length of the string, which is one larger than the largest index into the string (because indices start at 0). This is a legal argument tosubstring
because that method accepts an index equal to, but not greater than, the length of the string and interprets it to mean "the end of the string."