我想刪除字符串中的字符。但不是字符串中所有字符的元素。例。我想"|red|red|red|red|"
變成"red|red|red|red"
所以我想創建一個函數,檢查一個字符串的第一個和最後一個索引是否是一個特定的字符,並刪除它,如果它的情況。F#刪除/替換特定字符串索引中的字符
到目前爲止,我想出了這樣的事情:
let rec inputFormatter (s : string) : string =
match s.[1] with
|'|'|','|'.'|'-' -> // something that replaces the char with "" in the string s
(inputFormatter s)
|_ -> match s.[(String.length s)] with
|"|"|","|"."|"-" -> // same as above.
(inputFormatter s)
|_ -> s
誰能幫我找出我可以在我的函數寫?當然如果你覺得更方便,也歡迎你提出一個完全不同的功能。
在此先感謝!
爲什麼不使用字符串實例的現有'.Replace'方法? – Gustavo
,因爲它會刪除字符串中所有字符的元素?如果我去s.Replace(「|」,「」),那麼它將刪除字符串中的所有管道 – Nulle
Combine [string.IndexOf](https://msdn.microsoft.com/en-us/library/kwb0bwyd( v = vs.110).aspx),[string.LastIndexOf](https://msdn.microsoft.com/en-us/library/0w96zd3d(v = vs.110).aspx)與[string.Remove]( https://msdn.microsoft.com/en-us/library/d8d7z2kk(v=vs.110).aspx)。 –