2025年5月3日 星期六

函數:字串取代


在 MultiCharts 上對字串處理的內建函數有大、小寫互換、LeftStr、MidStr 等等,缺了在字串中指定字串更換的功能,也就是一般所謂的 String Replace。就... 做一個來用吧。


字串型函數,名稱:ˍStrReplace(參數1, 參數2, 參數3)

參數1:要被處理的字串
參數2:字串中要被更換掉的字元
參數3:字串中要更換成的字元

以下是效果範例:


這是函數的 code:

input: inputStr(string), Str_Find(string), Str_Replace(string);

var: resultStr(""), position(0), lenFind(0), tempStr("");


resultStr = inputStr;
lenFind = StrLen(Str_Find);

if lenFind > 0 then begin
  position = InStr(resultStr, Str_Find);
  while position > 0 begin
    tempStr = LeftStr(resultStr, position - 1) + Str_Replace + 
              RightStr(resultStr, StrLen(resultStr) - position - lenFind + 1);
    resultStr = tempStr;
    position = InStr(resultStr, Str_Find);
  end;
end;

_StrReplace = resultStr;


熱門文章