开发者

Delphi: Any StringReplaceW or WideStringReplace functions out there?

开发者 https://www.devze.com 2022-12-26 07:00 出处:网络
Are there any wide-string manipulation implementations out there? function WideUpperCase(const S: 开发者_Python百科WideString): WideString;

Are there any wide-string manipulation implementations out there?

function WideUpperCase(const S: 开发者_Python百科WideString): WideString;

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

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

etc


The JEDI project includes JclUnicode.pas, which has WideUpperCase and WidePos, but not StringReplace. The SysUtils.pas StringReplace code isn't very complicated, so you could easily just copy that and replace string with WideString, AnsiPos with WidePos, and AnsiUpperCase with WideUpperCase and get something functional, if slow.


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;


The TntControls has a set of Wide-version functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消