Linux 拨号vps windows公众号手机端

java中indexof的作用是什么

lewis 8年前 (2017-01-04) 阅读数 9 #程序编程
文章标签 Java

在Java中,indexOf()方法用于返回指定字符串在字符串中第一次出现的位置索引。如果找不到指定字符串,则返回-1。该方法的语法为:

int indexOf(String str)

其中,str为要查找的字符串。

例如:

String str = "Hello World";
int index = str.indexOf("World");
System.out.println(index); // 输出为 6

上面的例子中,indexOf()方法返回了"World"在字符串"Hello World"中的位置索引,即6。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门