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:从指定的索引开始,包含指定子字符串的情况

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

输出结果:9

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

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

输出结果:-1

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

  1. java.lang.String.indexOf()用法

    java.lang.String.indexOf(char ch) 方法返回字符ch在指定字符串中第一次出现的下标索引位置 如果字符ch在指定的字符串中找不到,则返回-1 示例: import jav ...

  2. Android——String.IndexOf 方法 (value, [startIndex], [count])

    报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置.  参数 value  要查找的 Unicode 字符. 对 value 的搜索区分大小写. startI ...

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

    c# String.IndexOf 方法 (value, [startIndex], [count]) 报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...

  4. 重写java.lang.String IndexOf()方法,实现对字符串以ASCII规则截取

    /** * 根据元数据和目标ascii位数截取字符串,失败返回-1 * @param sourceStr 元数据字符串 * @param endIndex 截取到第几位 * @return 结果字符串 ...

  5. 字符串查找String.IndexOf

    String.indexOf的模拟实现,没想象中有多么高深的查找算法,就是最普通的遍历查找 思路:先找到第一个相同的字符,然后依次比较后面的字符,若都相等则表示查找成功 /** * 查找字符串patt ...

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

    通常情况下,我们判断一个字符串中是否存在某值常常会用string.contains,其实判断一个字符串中存在某值的方法有很多种,最常用的就是前述所说的string.contains,相对来说比较常用的 ...

  7. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...

  8. java.lang.String 类的所有方法

    java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...

  9. String.IndexOf String.IndexOf String.Substring

    String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...

随机推荐

  1. vi编辑器的快捷键汇总

    光标控制命令 本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027& ...

  2. NOIP2019(CSP2019) 游记

    NOIP2019(CSP2019) 游记 近一年的似乎也就是感觉比别的学校的同学水平低的不止一点,到现在也没有搞清楚大概应该怎么科学有效的练习,并不会思考"为什么想不到"和&quo ...

  3. windows 安装apache2.4

    https://segmentfault.com/a/1190000008767607 一.下载 1.下载地址: http://httpd.apache.org/download.cgi 2.找到Fi ...

  4. 设计模式Design Pattern(1)--简介

    什么是设计模式? 软件开发人员在长期实践中总结出来的解决特定问题的一套解决方案. 对象设计原则 计模式主要是基于以下的面向对象设计原则. 对接口编程而不是对实现编程. 优先使用对象组合而不是继承. 设 ...

  5. zabbix监控A主机到B主机的网络质量

    采用zabbix自带的icmp ping即可进行监控: 1.安装fping 2.将fping安装后链接到/usr/sbin/fping下,设置组为zabbix; 3.增加监控项:icmpping[ip ...

  6. HTML中的表单<form>标签

    一.HTML表单 HTML 表单用于搜集不同类型的用户输入. HTML 表单包含表单元素,表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 关于表单的更多内容可以参考htt ...

  7. VS2015编译问题:模块对于 SAFESEH 映像是不安全的

    严重性 代码 说明 项目 文件 行 禁止显示状态 错误 LNK2026 模块对于 SAFESEH 映像是不安全的. zlibvc C:\MyDirectory\test2\zlib-\contrib\ ...

  8. Deque 容器

    1.Deque (1)定义 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除, ...

  9. Packet Transactions: High-level Programming for Line-Rate Switches

    Name of article:Packet Transactions: High-level Programming for Line-Rate Switches Origin of the art ...

  10. oracle 如何更改密码的hash

    如何迁移oracle user的密码到新的环境,一下列出了方法: select name,spare4||';'||password pwd from sys.user$ where name = ' ...