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)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...
随机推荐
- 值栈ValueStack的原理与生命周期
1.ValueStack贯穿整个 Action 的生命周期,保存在request域中,所以ValueStack和request的生命周期一样.当Struts2接受一个请求时,会迅速创建ActionCo ...
- mac下安装 rabbitMq
1.安装HomeBrew,如果已经安装这一步跳过. 2.用brew install rabbitmq指令即可进行rabbitmq服务的自动安装. 3.安装完成之后会出现一下提示: rabbit安装 ...
- 【python基础】集合方法汇总
一.定义 用于存储一组无序的不重复的数据 二.特点 1. 集合是无序的 2.集合中的元素是不重复的, 唯一的 3.集合中存储的数据必须是不可变的数据类型 4.集合是可变的数据类型 三.语法 set1 ...
- 【开车旅行】题解(NOIP2012提高组)
分析 首先我们可以发现,两个询问都可以通过一个子程序来求. 接着,如果每到一个城市再找下一个城市,显然是行不通的.所以首先先预处理从每一个城市开始,小A和小B要去的城市.预处理的方法很多,我用的是双向 ...
- WEB实现大文件上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- [LightOJ1240]Point Segment Distance 题解
题意简述 原题LightOJ 1240,Point Segment Distance(3D). 求三维空间里线段AB与C. 题解 我们设一个点在线段AB上移动,然后发现这个点与原来的C点的距离呈一个单 ...
- spring学习笔记之---JDBC Template
JDBC Template(简化持久化操作) (一)创建项目 (1)Maven配置 <dependencies> <dependency> <groupId>ju ...
- Menu Items are not showing on Action Bar
http://stackoverflow.com/questions/18010072/menu-items-are-not-showing-on-action-bar 版权声明:本文为博主原创文章, ...
- fengmiantu---
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第1节 Scanner类_2-概述及其API文档
键盘输入的类 Scanner首字母是大写的S.说明这是一个类,并不是关键字.关键字都是小写的.这个类是jdk中已经提供好的 1.看包 2.构造方法. 3.方法 只要不是引用类型,就是基本类型