java入门---字符串实例操作之字符串搜索&查找

xiaoxiao2021-02-28  22

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

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

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

luyaran 字符串位置 7
转载请注明原文地址: https://www.6miu.com/read-2450004.html

最新回复(0)