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)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...
随机推荐
- BAT面试必问题系列:深入详解JVM 内存区域及内存溢出分析
前言 在JVM的管控下,Java程序员不再需要管理内存的分配与释放,这和在C和C++的世界是完全不一样的.所以,在JVM的帮助下,Java程序员很少会关注内存泄露和内存溢出的问题.但是,一旦JVM发生 ...
- vue组件结构
1.组件结构 2.项目结构
- C# 实现二维数组的排序算法(代码)
class Order { /// <summary> /// 对二维数组排序 /// </summary> /// <param name="values&q ...
- SQL Server死锁问题:事务(进程 ID x)与另一个进程被死锁在 锁 | 通信缓冲区资源上并且已被选作死锁牺牲品。请重新运行该事务。
### The error occurred while setting parameters### SQL: update ERP_SCjh_zzc_pl set IF_TONGBU=1 where ...
- 洛谷p3956 棋盘(NOIP2017 t3)
在noip考场上本来以为只能骗暴力分,没想到最后A了: 本蒟蒻的做法比较简(zhi)单(zhang):记忆化深搜(考场上本来是想打广搜的,但我深搜稳一点就这样打了): 具体:每个点用一个f数组记录当前 ...
- django model 操作总结
使用场景 一对一:在某表中创建一行数据时,有一个单选的下拉框(下拉框中的内容被用过一次就消失了).//两个表的数据一一对应 例如:原有含10列数据的一张表保存相关信息,经过一段时间之后,10列无法满足 ...
- (转)CBC模式和ECB模式解读
一 什么是CBC模式 CBC模式的全称是Cipher Block Chaining模式(密文分组链接模式),之所以叫这个名字,是因为密文分组像链条一样相互连接在一起. 在CBC模式中,首先将明文分组与 ...
- qbzt day7上午
由于优盘咕咕咕了,所以这篇就咕咕咕了 以后还会补上的 qwq
- 纯CSS3写一个立方体并在鼠标悬停的时候无限循环旋转
- Mac开机自动运行shell脚本
1.首先写一个sh脚本,比如: cd ~/Documents mkdir haha 代码很简单,进入Documents文件夹,建立haha目录,保存为run.sh 2.修改run.sh权限 sudo ...