The point is: indexOf() takes a char, or a string; but not a regular expression. Show Thus: String input = "a\tb"; System.out.println(input); System.out.println(input.indexOf('\t'));prints 1 because there is a TAB char at index 1. System.out.println(input.indexOf("\\s+"));prints -1 because there is no substring \\s+ in your input value. In other words: if you want to use the powers of regular expressions, you can't use indexOf(). You would be rather looking towards String.match() for example. But of course - that gives a boolean result; not an index. If you intend to find the index of the first whitespace, you have to iterate the chars manually, like:
The String 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 IndexYou can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1. For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara. O roar again!"; char aChar = anotherPalindrome.charAt(9); Indices begin at 0, so the character at index 9 is 'O', as illustrated in the following figure: If you want to get more than one consecutive character from a string, you can use the substring method. The substring method has two versions, as shown in the following table:
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 StringsHere are several other String methods for manipulating strings:
Searching for Characters and Substrings in a StringHere are some other String methods for finding characters or substrings within a string. The String class provides accessor methods that return the position within the string of a specific character or substring: indexOf() and lastIndexOf(). The indexOf() methods search forward from the beginning of the string, and the lastIndexOf() methods search backward from the end of the string. If a character or substring is not found, indexOf() and lastIndexOf() 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.
Note: CharSequence is an interface that is implemented by the String class. Therefore, you can use a string as an argument for the contains() method. The String 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:
An ExampleThe following class, Filename, illustrates the use of lastIndexOf() and substring() to isolate different parts of a file name. Note: The methods in the following Filename 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. 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); } // gets filename without extension public String filename() { 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); } } Here is a program, FilenameDemo, that constructs a Filename object and calls all of its methods: public class FilenameDemo { public static void main(String[] args) { final String FPATH = "/home/user/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()); } } And here's the output from the program: Extension = html Filename = index Path = /home/user As shown in the following figure, our extension method uses lastIndexOf to locate the last occurrence of the period (.) in the file name. Then substring uses the return value of lastIndexOf 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 a StringIndexOutOfBoundsException. Also, notice that the extension method uses dot + 1 as the argument to substring. 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 to substring 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." |