Java 实例 - 字符串搜索
String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如果存在返回字符串出现的位置(第一位为0),如果不存在返回 -1:
SearchStringEmp.java 文件
public class SearchStringEmp {
public static void main(String[] args) {
String strOrig = "Google facesoho Taobao";
int intIndex = strOrig.indexOf("facesoho");
if(intIndex == - 1){
System.out.println("没有找到字符串 facesoho");
}else{
System.out.println("facesoho 字符串位置 " + intIndex);
}
}
}
public static void main(String[] args) {
String strOrig = "Google facesoho Taobao";
int intIndex = strOrig.indexOf("facesoho");
if(intIndex == - 1){
System.out.println("没有找到字符串 facesoho");
}else{
System.out.println("facesoho 字符串位置 " + intIndex);
}
}
}
输出结果为:
facesoho 字符串位置 7
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题