新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

java中indexof方法如何使用,java中indexof方法查找数值

发布时间:2024-03-14 16:55:24

java中indexof方法如何使用

在Java中,indexOf()方法用于查找指定元素在字符串中第一次出现的位置。方法的语法以下:

public int indexOf(String str)

参数str是要查找的字符串,方法返回值是指定字符串在原字符串中第一次出现的位置(索引),如果未找到,则返回⑴。

下面是一个示例代码:

String str = "Hello, World!";
int index = str.indexOf("World");
System.out.println("The index of 'World' in the string is: " + index);

在这个例子中,indexOf()方法将在字符串"Hello, World!"中查找子字符串"World"的位置,并将结果打印出来。