Jump to content
chkaufmann

function stripcslashes()

Recommended Posts

I need a function like stripcslashes() in PHP. Is there something similar in Delphi or do I have to write it on my own.

 

Christian

Share this post


Link to post
10 minutes ago, chkaufmann said:

I need a function like stripcslashes() in PHP. Is there something similar in Delphi or do I have to write it on my own.

 

Christian

The PHP function converts escaped characters like \n to their actual character (chr(13) in this case), no? Delphi has nothing like that since there is no need to escape characters in Delphi strings, they are all UTF-16 by default. So you have to write your own, I think.

Share this post


Link to post

Yes, that's what I need. Convert \n and \t to chr(10) and chr(9).

 

Treating the string as array is this fine? Or is there a faster way to do it? Something like this


 

  n := ASource.Length;
  SetLength(Result, n);
  ix1 := 1;
  ix2 := 1;
  while ix1 <= n do begin
    if ASource[ix1] = '\' then begin
      Inc(ix1);
      case ASource[ix1] of
        't': Result[ix2] := chr(9);
        'n': Result[ix2] := chr(10);
        '\': Result[ix2] := '\';
      end;
    end
    else 
      Result[ix2] := ASource[ix1];
    Inc(ix1);
    Inc(ix2);
  end; 
  if ix1 <> ix2
    then SetLength(Result, ix2 - 1);

 

Christian

Share this post


Link to post
Result := ASource.Replace('\n', #10).Replace('\t', #9);

Perhaps not the fastest approach, but pretty simple:.

Share this post


Link to post
22 hours ago, Uwe Raabe said:

Perhaps not the fastest approach, but pretty simple:.

KISS rules!

Share this post


Link to post

May be a bit unexpected approach. And sorry not C but JS, but they are close here.

uses
  System.JSON;

function StripJSSlashes(const AStr: string): string;
var
  LVal: TJSONValue;
begin
  LVal := TJSONObject.ParseJSONValue('"' + AStr + '"', False, True);
  try
    Result := TJSONString(LVal).Value;
  finally
    LVal.Free;
  end;
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×