Search code examples
delphiunicodestringdelphi-5

Delphi: Any StringReplaceW or WideStringReplace functions out there?


Are there any wide-string manipulation implementations out there?

function WideUpperCase(const S: WideString): WideString;

function WidePos(Substr: WideString; S: WideString): Integer;

function StringReplaceW(const S, OldPattern, NewPattern: WideString; 
      Flags: TReplaceFlags): WideString;

etc

Solution

  • I generally import the "Microsoft VBScript Regular Expression 5.5" type library and use IRegExp objects.

    OP Edit

    i like this answer, and i went ahead and wrote a StringReplaceW function using 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;