溫馨提示×

java怎么獲取某個字符串的位置

小億
147
2023-08-24 21:53:06
欄目: 編程語言

在Java中,可以使用String類的indexOf()方法來獲取某個字符串的位置。該方法的語法如下:

public int indexOf(String str)

以下是一個示例:

public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
int position = str.indexOf("World");
System.out.println("The position of 'World' is: " + position);
}
}

輸出結果為:

The position of 'World' is: 7

在這個例子中,indexOf()方法返回了字符串"World"在原始字符串中的位置,即7。如果原始字符串中不存在該子字符串,indexOf()方法將返回-1。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女