解題
題目:https://leetcode.com/problems/remove-all-occurrences-of-a-substring/
目標:删除s字串内所有出现的part
使用IndexOf(string)回報字串第一次出現時的所在索引 (以零為起始)
再來用while迴圈做移除字元Remove(索引, part字串長度)並重新回報字串第一次出現時的所在索引
若已無出現part相似字則輸出剩下字串
程式碼
public class Solution {
public string RemoveOccurrences(string s, string part) {
int i = s.IndexOf(part);
while(i!=-1){
s= s.Remove(i, part.Length);
i = s.IndexOf(part);
}
return s;
}
}