共計 378 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 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。
丸趣 TV 網(wǎng) – 提供最優(yōu)質的資源集合!
正文完