zoukankan      html  css  js  c++  java
  • 【转】Java中字符串中子串的查找共有四种方法(indexof())

    原文网址:http://wfly2004.blog.163.com/blog/static/1176427201032692927349/

    Java中字符串中子串的查找共有四种方法,如下:
    1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。 
    2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 
    3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。 
    4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

    indexof()用法说明
    indexof()

    返回 String 对象内第一次出现子字符串的字符位置。

    string.indexOf(subString[, startIndex])

    参数 
    string

    必选项。String 对象或文字。

    subString 必选项。

    要在 String 对象中查找的子字符串。

    starIndex 可选项。

    该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。

    说明 
    indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。

    如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

    从左向右执行查找。否则,该方法与 lastIndexOf 相同。

    示例 
    下面的示例说明了 indexOf 方法的用法。

    function IndexDemo(str2){ 
    var str1 = "BABEBIBOBUBABEBIBOBU" 
    var s = str1.indexOf(str2); 
    return(s); 
    }

  • 相关阅读:
    例题6-8 Tree Uva548
    例题6-7 Trees on the level ,Uva122
    caffe Mac 安装
    Codeforces Round #467 (Div. 1) B. Sleepy Game
    Educational Codeforces Round37 E
    Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
    Good Bye 2017 E. New Year and Entity Enumeration
    Good Bye 2017 D. New Year and Arbitrary Arrangement
    Codeforces Round #454 D. Seating of Students
    浙大紫金港两日游
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4458496.html
Copyright © 2011-2022 走看看