Java String indexOf(String substr,int fromIndex)方法,带示例

字符串indexOf(String substr,int fromIndex)方法 (String indexOf(String substr, int fromIndex) Method)

indexOf(String substr, int fromIndex) is a String method in Java and it is used to get the index of a specified substring in the string from given fromIndex. That means to search for the substring will start from the given index (fromIndex).

indexOf(String substr,int fromIndex)是Java中的String方法,用于从给定的fromIndex获取字符串中指定子字符串的索引。 这意味着从指定索引( fromIndex )开始搜索子字符串。

If substring exists in the string from fromIndex, it returns the index of the first occurrence of the substring, if substring does not exist in the string, it returns -1.

如果从的fromIndex字符串中存在串,它返回字符串的第一个出现的位置,如果子字符串没有字符串中存在,则返回-1。

Syntax:

句法:

    int str_object.indexOf(String substr, int fromIndex);

Here,

这里,

  • str_object is an object of main string in which we have to find the index of given substring.

    str_object是主字符串的对象,在其中我们必须找到给定子字符串的索引。

  • substr is the part of the string to be found in the string.

    substr是要在字符串中找到的字符串的一部分。

  • fromIndex is an index of the main string, from where method will start searching for the substring.

    fromIndex是主字符串的索引,方法将从该索引开始搜索子字符串。

It accepts a substring, from index and returns index of its first occurrence or -1 if substring does not exist in the string.

它从索引接受子字符串,并返回其第一次出现的索引;如果字符串中不存在子字符串,则返回-1。

Example:

例:

    Input: 
String str = "IncludeHelp"
Function call:
str.indexOf("Help", 4)
Output:
7
Input: 
String str = "IncludeHelp"
Function call:
str.indexOf("HELP, 2)
Output:
-1

Java code to demonstrate the example of String.indexOf() method

Java代码演示String.indexOf()方法的示例

public class Main
{
public static void main(String[] args) {
String str = "IncludeHelp";
String substr = "";
int index;
substr = "Help";
index = str.indexOf(substr, 4);
if(index != -1)
System.out.println(substr + " is found at " + index + " position.");
else 
System.out.println(substr + " does not found.");
substr = "lude";
index = str.indexOf(substr, 8);
if(index != -1)
System.out.println(substr + " is found at " + index + " position.");
else 
System.out.println(substr + " does not found.");            
substr = "HELP";
index = str.indexOf(substr, 2);
if(index != -1)
System.out.println(substr + " is found at " + index + " position.");
else 
System.out.println(substr + " does not found.");
}
}

Output

输出量

Help is found at 7 position.
lude does not found.
HELP does not found.

翻译自: https://www.includehelp.com/java/string-indexOf-String-substr-int-fromindex-method-with-example.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546081.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

zoj 1078 palindrom numbers

题目见zoj 1078 主要是判断一个整数在基数为2-16之间的某个数字时是否为回文,我是直接该整数转换成对应基数的表示的逆序列,并计算出该表示下的值,判断是否等于这个整数值,如果相等,那么就是回文,如果不相…

iredmail邮件服务器之修改默认的web服务端口号

安装iredmail之后,由于需要在路由器上做端口映射以便在外网访问webmail,因此端口不能和WEB服务的端口好冲突,所以需要修改邮件服务器的httpd服务的端口。 一、apache/httpd的http服务和https服务端口号都要修改。 基本服务端口好办&#xff0…

轻松学算法的秘密!可视化算法网站汇总!(附动图)

对于「算法」的第一印象,我相信大部分人都是一样的,就是一个“难”字了得。而我比较特殊,我的第一印象、第二印象以至第 N 印象都觉得很难,所以为了更好的学习和理解算法,我千金一掷一下买了一堆的算法书,有…

从100套真题中提炼而出的100个经典句子

1. Typical of the grassland dwellers of the continent is the American antelope, or pronghorn. 1.美洲羚羊,或称叉角羚,是该大陆典型的草原动物。 2. Of the millions who saw Haley’s comet in 1986, how many people will live long enough to s…

字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写

字符串大写转小写库函数Given a string and we have to convert it into uppercase string without using any library function. 给定一个字符串&#xff0c;我们必须在不使用任何库函数的情况下将其转换为大写字符串。 PHP code: PHP代码&#xff1a; <?php//function …

zoj 1091 Knight Moves

题目见zoj 1091 使用宽度搜索优先来求解&#xff0c;这个算法已经忘记的差不多了&#xff0c;所以写出来的代码很罗嗦&#xff0c;看起来很不清晰。 好像还可以直接用公式或者神经网络算法求解,详见Knights Tour /* zoj 1091 Knight Moves */ #include <stdio.h> #incl…

1.基本类型的指针

例子一&#xff1a; #include<stdio.h>int main() {int i10;int * p&i; //p是个变量名字&#xff0c;int * 表示该p变量只能存储int类型变量的地址。//int *p&i;等价于 int *p; p&i;//p存放了i的地址,p指向i,*p就是i变量本身printf("%d\n",p);// …

VB中KeyCode常数用法 VB 按键

VB中KeyCode常数用法可在代码中的任何地方用下列常数代替实际值&#xff1a;常数 值 描述vbKeyLButton 0x1 鼠标左键vbKeyRButton 0x2 鼠标右键vbKeyCancel 0x3 CANCEL 键vbKeyMButton 0x4 鼠标中键vbKeyBack 0x8 BACKSPACE 键vbKeyTab 0x9 TAB 键vbKeyClear 0xC CLEAR 键vbKey…

zoj 1088 System Overload

约瑟夫环 (josephus problem &#xff09;问题&#xff0c;有公式 可以直接套用 我使用暴力破解方法求解&#xff08;用时3秒多&#xff09;。 代码如下&#xff1a; /* zoj 1088 System Overload */ #include <stdio.h> #include <string.h>#define MAXBUILDING …

链表竟然比数组慢了1000多倍?(动图+性能评测)

这是我的第 215 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;数组和链表是程序中常用的两种数据结构&#xff0c;也是面试中常考的面试题之一。然而对于很多人来说…

c语言用宏定义常量_使用宏定义常量以在C的数组声明中使用

c语言用宏定义常量As we know that, while declaring an array we need to pass maximum number of elements, for example, if you want to declare an array for 10 elements. You need to pass 10 while declaring. Example: int arr[10]; 众所周知&#xff0c;在声明数组时…

HTML字体颜色设置

html字体颜色设置behavior移动效果>插入文字,alternate(交替滚动)、slide(幻灯片效果,指的是滚动一次,然后停止滚动) scrollAmount"移动速度" direction"方向"字体颜色设置字体< f o n t f a c e 宋 体 c o l o r # 9 9 3 2 c c s i z e 5 > 字…

zoj 1025 Wooden Sticks

题目见&#xff1a;zoj 1025 先对木棒按照长度进行排序&#xff0c;然后再计算对应重量构成的数组中非递减子序列的个数。 相关代码&#xff08;悲催的是该代码不能在poj1065 和hdoj1051 下通过&#xff0c;懒得看具体是什么原因了&#xff09; /* zoj 1025 Wooden sticks *…

win7安装python

2019独角兽企业重金招聘Python工程师标准>>> 官网下载 python 安装包&#xff0c;一个 .msi 文件&#xff0c;双击安装 右键[计算机]-属性-高级系统设置-环境变量&#xff0c;在[系统变量]中找到 Path 变量&#xff0c;双击&#xff0c;在值后面添加 python 的安装路…

漫画:Java如何实现热更新?

Arthas&#xff08;阿尔萨斯&#xff09;是 Alibaba 开源的一款 Java 诊断工具&#xff0c;使用它我们可以监控和排查 Java 程序&#xff0c;然而它还提供了非常实用的 Java 热更新功能。所谓的 Java 热更新是指在不重启项目的情况下实现代码的更新与替换。使用它可以实现不停机…

十进制数转换为十六进制数_十进制数制到十六进制数制的转换

十进制数转换为十六进制数Conversion of decimal number system into hexadecimal number system can be done by successively dividing an integral part by 16 till the quotient is 0 and then reading the remainder of all in the bottom to the top manner, where the b…

编译器开发相关资源

开发编译器相关的一些网络资源&#xff1a; how difficult is it to write a compiler? wikipedia compiler compiler 101 code competetion pl/0 (couses &#xff0c;source code ,another source code )and pl/I language the gentle compiler construction sys…

计算机二级考试C++考试大纲

基本要求: 1. 掌握C语言的基本语法规则。 2. 熟练掌握有关类与对象的相关知识。 3. 能够阅读和分析C程序。 4. 能够采用面向对象的编程思路和方法编写应用程序。 5. 能熟练使用Visual C6.0集成开发环境编写和调度程序。 考试内容&#xff1a; 一、 C语言概述 1. 了解C语言的基本…

自动化运维工具Saltstack详细介绍

Saltstack是一个新的基础设施管理工具。目前处于快速发展阶段&#xff0c;可以看做是pssh弱化的Puppet的组合。间接的反映出了saltstack的两大功能&#xff1a;远程执行和配置管理。Saltstack使用Python开发&#xff0c;是一个非常简单易用和轻量级的管理工具。由Master和Minio…