

The Search Methods in the String Class Method The following table describes the various string search methods. Use this method when you only need to know that the string contains a character sequence, but the precise location isn't important. The String class also provides a search method, contains, that returns true if the string contains a particular character sequence. If a character or substring is not found, indexOf() and lastIndexOf() return -1. The indexOf() methods search forward from the beginning of the string, and the lastIndexOf() methods search backward from the end of the string. The String class provides accessor methods that return the position within the string of a specific character or substring: indexOf() and lastIndexOf(). Here are some other String methods for finding characters or substrings within a string.

Searching for Characters and Substrings in a String If no conversions are necessary, these methods return the original string.

Returns a copy of this string converted to lowercase or uppercase. Returns a copy of this string with leading and trailing white space removed. Returns a new character sequence constructed from beginIndex index up until endIndex - 1. Regular expressions are covered in the lesson titled "Regular Expressions."ĬharSequence subSequence(int beginIndex, int endIndex) The optional integer argument specifies the maximum size of the returned array. 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. Here are several other String methods for manipulating strings: Other Methods in the String Class for Manipulating Strings Method String roar = anotherPalindrome.substring(11, 15)
#JAVA HIIDE A STRING CODE#
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": Here, the returned substring extends to the end of the original string. The integer argument specifies the index of the first character. Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. String substring(int beginIndex, int endIndex) The substring method has two versions, as shown in the following table: The substring Methods in the String Class Method If you want to get more than one consecutive character from a string, you can use the substring method. Indices begin at 0, so the character at index 9 is 'O', as illustrated in the following figure: O roar again!" Ĭhar aChar = anotherPalindrome.charAt(9)
