How to find all positions of one string in another string in swift2 ?

后端 未结 4 1393
执念已碎
执念已碎 2021-01-02 06:12

I can find first position of string \"ATG\" in myString \"ATGGACGTGAGCTGATCGATGGCTGAAATGAAAA\" (i.e. index range is 0..<3) by using code below. Question is how to find al

4条回答
  •  萌比男神i
    2021-01-02 06:45

    That makes sense, because according to the docs, rangeOfString:

    Finds and returns the range of the first occurrence of a given string within the receiver.

    If you'd like to find all the occurrences, you could loop until rangeOfString: returns nil, and each time—trim the string up to just after the matched range. You'd have to keep track of your position within the original string and transpose the indexes, of course.

提交回复
热议问题