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> >= 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()的使用方法的更多相关文章
- java.lang.String.indexOf()用法
java.lang.String.indexOf(char ch) 方法返回字符ch在指定字符串中第一次出现的下标索引位置 如果字符ch在指定的字符串中找不到,则返回-1 示例: import jav ...
- Android——String.IndexOf 方法 (value, [startIndex], [count])
报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. 参数 value 要查找的 Unicode 字符. 对 value 的搜索区分大小写. startI ...
- c# String.IndexOf 方法 string查找字符串
c# String.IndexOf 方法 (value, [startIndex], [count]) 报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...
- 重写java.lang.String IndexOf()方法,实现对字符串以ASCII规则截取
/** * 根据元数据和目标ascii位数截取字符串,失败返回-1 * @param sourceStr 元数据字符串 * @param endIndex 截取到第几位 * @return 结果字符串 ...
- 字符串查找String.IndexOf
String.indexOf的模拟实现,没想象中有多么高深的查找算法,就是最普通的遍历查找 思路:先找到第一个相同的字符,然后依次比较后面的字符,若都相等则表示查找成功 /** * 查找字符串patt ...
- 字符串中判断存在的几种模式和效率(string.contains、string.IndexOf、Regex.Match)
通常情况下,我们判断一个字符串中是否存在某值常常会用string.contains,其实判断一个字符串中存在某值的方法有很多种,最常用的就是前述所说的string.contains,相对来说比较常用的 ...
- 关于JAVA的String类的一些方法
一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...
- java.lang.String 类的所有方法
java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...
- String.IndexOf String.IndexOf String.Substring
String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...
随机推荐
- CF1260C Infinite Fence 题解(扩欧)
题目地址 CF1260C 题目大意 现有\(10^{100}\)块木板需要涂漆,第x块如果是x是a的倍数,则涂一种颜色,是b的倍数,则涂另一种颜色.如果既是a又是b的倍数,那么两种颜色都可以涂:如果连 ...
- Markdown的使用和计算机基础
TOC] 一级标题 这不是开玩笑 你问我为什么? 粗的才好(滑稽) 什么!明明有人推我 ==一闪一闪亮晶晶== 我上面有人^人在这^ water?H~2~O(下标) hello world! hell ...
- 关于sword框架浏览器上方小图标的修改
sword框架默认有一个document.ejs文件,可以导出html模板(找了很久没找到index.html,哈哈哈),里面有一行代码 这个href就是代表着浏览器上方图标的路径. 在public文 ...
- linux运维、架构之路-Kubernetes集群部署
一.kubernetes介绍 Kubernetes简称K8s,它是一个全新的基于容器技术的分布式架构领先方案.Kubernetes(k8s)是Google开源的容器集群管理系统(谷歌内部 ...
- POJ 3764 The xor-longest Path ( 字典树求异或最值 && 异或自反性质 && 好题好思想)
题意 : 给出一颗无向边构成的树,每一条边都有一个边权,叫你选出一条路,使得此路所有的边的异或值最大. 分析 : 暴力是不可能暴力的,这辈子不可能暴力,那么来冷静分析一下如何去做.假设现在答案的异或值 ...
- Acitiviti的查询及删除(六)
流程定义查询 查询部署的流程定义. /** * 查询流程定义信息 //act_re_procdef */ public class QueryProcessDefinition { public st ...
- Leetcode 2. Add Two Numbers(指针和new的使用)结构体指针
---恢复内容开始--- You are given two non-empty linked lists representing two non-negative integers. The di ...
- Python_020(几个经典内置方法)
一.内置方法 1.内置方法表示:__名字__ 几种名称: 1)双下方法 2)魔术方法 3)类中的特殊方法/内置方法 类中的每一个双下方法都有它自己的特殊意义;所有的双下方法没有 需要你在外部直接调用的 ...
- 在github pages网站下用jekyll制作博客教程
https://www.jekyll.com.cn/ https://github.com/onevcat/vno-jekyll https://help.github.com/articles/us ...
- RabbitMQ绑定、队列、消息、虚拟主机详解(五)
Binding:绑定,Exchange和Exchange.Queue之间的连接关系 Binding中可以包含RoutingKey或者参数 Queue:消息队列,实际存储消息数据 Durability: ...