Ian Boyd
Ian Boyd

Reputation: 257095

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

Upvotes: 0

Views: 3495

Answers (4)

Eugene X
Eugene X

Reputation: 149

uses WideStrUtils;
function WideStringReplace(const S, OldPattern, NewPattern: Widestring; Flags: TReplaceFlags): Widestring;

Upvotes: 1

Stijn Sanders
Stijn Sanders

Reputation: 36850

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;

Upvotes: 2

stanleyxu2005
stanleyxu2005

Reputation: 8241

The TntControls has a set of Wide-version functions.

Upvotes: 2

Zoë Peterson
Zoë Peterson

Reputation: 13332

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.

Upvotes: 4

Related Questions