Javaの例 - 文字列検索
次の例は、このような文字列の出現位置を超えるリターンの存在として、表示される文字列の部分に位置を見つけるために、StringクラスのindexOf()メソッドを使用していますノーリターン-1が存在しない場合、(最初のビット0です):
//SearchStringEmp.java 文件 public class SearchStringEmp{ public static void main(String[] args) { String strOrig = "Hello readers"; int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } } }
出力は、上記のコード例です:
Found Hello at index 0