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

2007-09-22 11:57 pm
例如:
我要搵mo既 position,
係string 內容係 momomo 既string入面,

佢地既position係 0,2,4,
請問點樣可以搵到0,2,4出來 ??

回答 (1)

2007-09-23 2:52 pm
✔ 最佳答案
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.


收錄日期: 2021-04-25 20:33:48
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070922000051KK02890

檢視 Wayback Machine 備份