Anonymous asked in 電腦及互聯網程式編寫及設計 · 1 decade ago

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


我要搵mo既 position,

係string 內容係 momomo 既string入面,

佢地既position係 0,2,4,

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

1 Answer

  • 1 decade ago
    Favorite Answer

    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.

Still have questions? Get your answers by asking now.