lastIndexOf(int ch) last position of ch
lastIndexOf(int ch, int start) last position of ch start
lastIndexOf(String str) last position of str
lastIndexOf(String str, int start) last position of str start
The indexing methods that take an int parameter for the character to look for will search for the given
character if the value is less than 0xFFFF, or else the code point with the given valuesee "Working with
UTF-16" on page 336.
If you don't care about the actual index of the substring, you can use the contains method, which returns
TRue if the current string contains a given CharSequence as a subsequence. If you want to find the index
of an arbitrary CharSequence you must invoke toString on the CharSequence and pass that to
indexOf instead.
Exercise 13.1: Write a method that counts the number of occurrences of a given character in a string.
Exercise 13.2: Write a method that counts the number of occurrences of a particular string in another string.
13.2.2. String Comparisons
The String class supports several methods to compare strings and parts of strings. Before we describe the
methods, though, you should be aware that internationalization and localization issues of full Unicode strings
are not addressed with these methods. For example, when you're comparing two strings to determine which is
"greater," characters in strings are compared numerically by their Unicode values, not by their localized
notion of order. To a French speaker, c and ç are the same letter, differing only by a small diacritical mark.
Sorting a set of strings in French should ignore the difference between them, placing "açb" before "acz"
because b comes before z. But the Unicode characters are differentc (\u0063) comes before ç (\u00e7)
in the Unicode character setso these strings will actually sort the other way around. Internationalization and
localization are discussed in Chapter 24.
The first compare operation is equals, which returns true if it is passed a reference to a String object
having the same contentsthat is, the two strings have the same length and exactly the same Unicode
characters. If the other object isn't a String or if the contents are different, String.equals returns
false. As you learned on page 100, this overrides Object.equals to define equivalence instead of
identity.
To compare strings while ignoring case, use the equalsIgnoreCase method. By "ignore case," we mean
that Ë and ë are considered the same but are different from E and e. Characters with no case distinctions, such
as punctuation, compare equal only to themselves. Unicode has many interesting case issues, including a
notion of "titlecase." Case issues in String are handled in terms of the case-related methods of the
Character class, as described in "Character" on page 192.
A String can be compared with an arbitrary CharSequence by using the contentEquals method,
which returns true if both objects represent exactly the same sequence of characters.
To sort strings, you need a way to order them, so String implements the interface
Comparable
returns an int that is less than, equal to, or greater than zero when the string on which it is invoked is less
than, equal to, or greater than the other string. The ordering used is Unicode character ordering. The String
class also defines a compareToIgnoreCase method.