String.indexOf()的使用方法

String.indexOf()的用途:

返回此字符串中第一个出现的指定的子字符串,如果没有找到则返回-1

源码如下:

/** * Returns the index within this string of the first occurrence of the * specified substring. * * <p>The returned index is the smallest value <i>k</i> for which: * <blockquote><pre> * this.startsWith(str, <i>k</i>) * </pre></blockquote> * If no such value of <i>k</i> exists, then {@code -1} is returned. * * @param   str   the substring to search for. * @return  the index of the first occurrence of the specified substring, *          or {@code -1} if there is no such occurrence. */
public int indexOf(String str) {
        return indexOf(str, 0);
    }

举例1:包含指定子字符串的情况

String str1="abcdef";
String str2="cd";
System.out.println(str1.indexOf(str2));

输出结果:2

举例2:未包含指定子字符串的情况

String str1="abcdef";
String str2="gh";
System.out.println( str1.indexOf(str2) );

输出结果:-1

它还有一个重载的方法:从指定的索引开始,返回此字符串中第一个出现的指定的子字符串,如果没有找到则返回-1

源码如下:

/**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.
     *
     * <p>The returned index is the smallest value <i>k</i> for which:
     * <blockquote><pre>
     * <i>k</i> &gt;= fromIndex {@code &&} this.startsWith(str, <i>k</i>)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then {@code -1} is returned.
     *
     * @param   str         the substring to search for.
     * @param   fromIndex   the index from which to start the search.
     * @return  the index of the first occurrence of the specified substring,
     *          starting at the specified index,
     *          or {@code -1} if there is no such occurrence.
     */
    public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }

举例3:从指定的索引开始,包含指定子字符串的情况

1 String str3="abcdef0abcdef";
2 String str4="cd";
3 System.out.println( str3.indexOf(str4,5) );

输出结果:9

举例3:从指定的索引开始,未包含指定子字符串的情况

String str3="abcdef0abcdef";
String str4="gh";
System.out.println( str3.indexOf(str4,5) );

输出结果:-1

原文地址:https://www.cnblogs.com/andrew-303/p/11652497.html

时间: 2024-08-29 10:56:15

String.indexOf()的使用方法的相关文章

java.lang.String.indexOf()用法

java.lang.String.indexOf(char ch) 方法返回字符ch在指定字符串中第一次出现的下标索引位置 如果字符ch在指定的字符串中找不到,则返回-1 示例: import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "This is tanglc's cnblog"; // returns the index of char

indexOf(String.indexOf 方法)

indexOf(String.indexOf 方法) 字符串的IndexOf()方法搜索在该字符串上是否出现了作为參数传递的字符串,假设找到字符串,则返回字符的起始位置 (0表示第一个字符,1表示第二个字符依此类推)假设说没有找到则返回 -1 返回 String 对象内第一次出现子字符串的字符位置. [code=csharp]public indexOf(value:String, [startIndex:Number]) : Number[/code] 搜索字符串,并返回在调用字符串内 sta

c# String.IndexOf 方法 string查找字符串

c# String.IndexOf 方法 (value, [startIndex], [count]) 报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. 参数 value 要查找的 Unicode 字符. 对 value 的搜索区分大小写. startIndex(Int32) 可选项,搜索起始位置.不设置则从0开始. count(Int32) 可选项,要检查的字符位数. 返回值 如果找到该字符,则为 value 的索引位置:否则如果未找到,则为 -1

关于JAVA的String类的一些方法

一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2.StringBuffer类的capacity()方法与String类的 length()的方法类似,但是她测试是分配给StringBuffer的内存空间的大小,而不是当前被使用了的内存空间. 3.如果想确定字符串中指定字符或子字符串在给定字符串的位置,可以用 indexOf()和lastIndexO

【java】String类的基本方法

Java的String类基本方法 一.构造函数 函数 返回值 作用 String(byte[] bytes) String 通过byte数组构造字符串对象 String(char[] chars) String 通过char数组构造字符串对象 String(String string) String 拷贝一个值为string的字符串对象 String(StringBuffer buffer) String 通过StringBuffer构造字符串对象 二.String类的基本方法 函数 返回值 作

学习中 常用到的string内置对象方法的总结

//concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. var str = "Hello"; var out = str.concat(" World","!"); console.log(str); //Hello console.log(out); //Hello World! //charAt() – 返回指定位置的字符. var str = "HelloString"; var out = st

字符串查找String.IndexOf

String.indexOf的模拟实现,没想象中有多么高深的查找算法,就是最普通的遍历查找 思路:先找到第一个相同的字符,然后依次比较后面的字符,若都相等则表示查找成功 /** * 查找字符串pattern在str中第一次出现的位置 * @param str * @param pattern * @return */ public int firstIndexOf(String str, String pattern) { for (int i = 0; i < (str.length() -

字符串中判断存在的几种模式和效率(string.contains、string.IndexOf、Regex.Match)

通常情况下,我们判断一个字符串中是否存在某值常常会用string.contains,其实判断一个字符串中存在某值的方法有很多种,最常用的就是前述所说的string.contains,相对来说比较常用的还有string.IndexOf和Regex.Match.直接上代码,后面在说些什么吧,通常情况下功能的实现最重要,作者的话,只对有心者有效. using System; using System.Collections.Generic; using System.Linq; using Syste

java.lang.String 类的所有方法

java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点). int codePointBefore(int index) 返回指定索引之前的字符(Unicode 代码点). int codePointCount(int beginIndex, int endIndex) 返回此 String 的指定文本范围中的 Un