zoukankan      html  css  js  c++  java
  • java之字符串中查找字串的常见方法

    1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。 
         int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 

    函数数名:indexOf

    调用方式:Object(String).indexOf(String str,int startIndex)或String.indexOf(String str)

    参数说明:str需要查找的字串.

                  startIndex 从指定的索引处开始查询,if (startIndex<0),则在程序执行中认为startIndex=0;

    if(startIndex>Object.length) 则它被当作最大的可能索引。then 正常查询。

    返回内容:if (在Object中查找到字串)返回字串第一次出现的索引

                if(在Object中没有查找到字串) return -1

    返回值类型:int

    example:

    /**
     * <p>Title:LookSubstring</p>
     * <p>This program demostrate "look for a substring from known String"</p>
     * <p>Filename:LookSubstring.java </p>
     * @ author 14941
     * @ version 1.0
     */
    public class LookSubstring
    {
     public static void main(String[] args)
     {
         //define a known String
      String str="assfdsffeffeffds";
      //define a substring
      String sustr="ff";
      System.out.println(str.indexOf(sustr));
      System.out.println(str.indexOf(sustr,8));
     }

    }

    result:

    6
    9


    2、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。 
         int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/rrttp/p/6502103.html
Copyright © 2011-2022 走看看