代码之家  ›  专栏  ›  技术社区  ›  Ian Boyd

德尔菲:有stringreplacew或widestringreplace函数吗?

  •  0
  • Ian Boyd  · 技术社区  · 14 年前

    有没有宽字符串操作的实现?

    function WideUpperCase(const S: WideString): WideString;
    
    function WidePos(Substr: WideString; S: WideString): Integer;
    
    function StringReplaceW(const S, OldPattern, NewPattern: WideString; 
          Flags: TReplaceFlags): WideString;
    
    etc
    
    3 回复  |  直到 14 年前
        1
  •  2
  •   Ian Boyd    14 年前

    我通常导入“microsoft vbscript正则表达式5.5”类型库并使用iregexp对象。

    操作编辑

    我喜欢这个答案,然后我写了一个 StringReplaceW 使用regex的函数:

    function StringReplaceW(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString;
    var
        objRegExp: OleVariant;
        Pattern: WideString;
        i: Integer;
    begin
        {
            Convert the OldPattern string into a series of unicode points to match
            \uxxxx\uxxxx\uxxxx
    
                \uxxxx  Matches the ASCII character expressed by the UNICODE xxxx.
                            "\u00A3" matches "£".
        }
        Pattern := '';
        for i := 1 to Length(OldPattern) do
            Pattern := Pattern+'\u'+IntToHex(Ord(OldPattern[i]), 4);
    
        objRegExp := CreateOleObject('VBScript.RegExp');
        try
            objRegExp.Pattern := Pattern;
            objRegExp.IgnoreCase := (rfIgnoreCase in Flags);
            objRegExp.Global := (rfReplaceAll in Flags);
    
            Result := objRegExp.Replace(S, NewPattern);
        finally
            objRegExp := Null;
        end;
    end;
    
        2
  •  4
  •   Zoë Peterson RRUZ    14 年前

    这个 JEDI project 包括jclunicode.pas,它有wideuppercase和widepos,但没有stringreplace。pas string replace代码并不复杂,因此您可以很容易地复制它,并用widestring替换string,用widepos替换ansipos,用wideuppercase替换ansiuppercase,如果速度慢的话,可以得到一些功能性的代码。

        3
  •  2
  •   stanleyxu2005    14 年前

    tntcontrols有一组宽版本函数。