Java 教程 Java 实例 - 字符串搜索

elroy · 2022-06-10 23:42:15 · 热度: 9

以下实例使用了 String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如果存在返回字符串出现的位置(第一位为0),如果不存在返回 -1:

SearchStringEmp.java 文件

public class SearchStringEmp { public static void main(String[] args) { String strOrig = "Google Codercto Taobao"; int intIndex = strOrig.indexOf("Codercto"); if(intIndex == - 1){ System.out.println("没有找到字符串 Codercto"); }else{ System.out.println("Codercto 字符串位置 " + intIndex); } } }

以上代码实例输出结果为:

Codercto 字符串位置 7

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册