Java文字列lastIndexOf()メソッド
例
「惑星」の最後の出現を文字列で検索します。
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.lastIndexOf("planet"));
定義と使用法
このlastIndexOf()
メソッドは、文字列内で最後に出現した文字の位置を返します。
ヒント:indexOfメソッドを 使用して、文字列内で最初に出現する文字の位置を返します。
構文
4つのlastIndexOf()メソッドがあります。
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
public int lastIndexOf(int char)
public int lastIndexOf(int char, int fromIndex)
パラメータ値
Parameter | Description |
---|---|
str | A String value, representing the string
to search for |
fromIndex | An int value, representing the index position to start the search from. If omitted, the default value is the length of the string |
char | An int value, representing a single
character, e.g 'A', or a Unicode value |
技術的な詳細
戻り値: | 文字列内で最初に出現するint 文字のインデックスを表す値、または出現しない場合は-1 |
---|
その他の例
例
文字列内で最後に出現する「e」を検索し、位置5から検索を開始します。
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.lastIndexOf("e", 5));
}
}
❮文字列メソッド