CBB 10 中的多个 StringReplace

Multiple StringReplace in CBB 10

我有 10 多个不同的字符串要替换。现在,我正在使用如下代码所示的手动方法。

StringReplace(StringReplace(StringReplace(StringReplace(
StringReplace(StringReplace(StringReplace(MyEditor->Text,
"one","ab",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase),
"two","cd",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase),
"three","ef",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase),
"four","gh",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase),
"five","ij",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase),
"six","kl",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase),
"seven","mn",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase);

...so on and on.

所以,我想知道如何使用更实用和灵活的方法来替换许多字符串。也许通过使用特定功能?。

非常感谢。

RTL 没有用于该目的的函数。你必须自己写,例如:

struct StringReplacePattern
{
    String OldPattern;
    String NewPattern;
};

String __fastcall MultiStringReplace(const String S, const StringReplacePattern *Patterns, int NumPatterns, TReplaceFlags Flags)
{
    String Result = S;
    for (int i = 0; i < NumPatterns; ++i)
        Result = StringReplace(Result, Patterns[i].OldPattern, Patterns[i].NewPattern, Flags);
    return Result;
}

那么你可以这样称呼它:

StringReplacePattern patterns[] = {
    {"one", "ab"},
    {"two", "cd"},
    {"three", "ef"},
    {"four", "gh"},
    {"five", "ij"},
    {"six", "kl"},
    {"seven", "mn"},
    // and so on...
};

String S = MultiStringReplace(MyEditor->Text, patterns, ARRAYSIZE(patterns), TReplaceFlags()<< rfReplaceAll << rfIgnoreCase);