匿名
匿名 發問於 電腦及互聯網程式編寫及設計 · 1 十年前

java : 點樣可以係條string度搵返哂所有substring 既position????

例如:

我要搵mo既 position,

係string 內容係 momomo 既string入面,

佢地既position係 0,2,4,

請問點樣可以搵到0,2,4出來 ??

1 個解答

評分
  • 1 十年前
    最愛解答

    Use indexOf(String str, int fromIndex) method of the String class. It returns the index of the first occurrence of the substring within the searched string, starting from the specified index.

    i = myString.indexOf(substring, 0)

    Begin fromIndex with 0. Every time a substring is found, continue finding the substring by putting i+(length of the substring) to fromIndex

    i = myString.indexOf(substring, i+substring.length)

    Continue until indexOf returns -1, that is, substring is not found.

還有問題嗎?立即提問即可得到解答。