Delphi 将字符串转换为C-escape转义字符串格式


本文整理自网络,侵删。

 
const
// Special symbols
  _TAB_   = #9;
  _CR_    = #13;
  _NL_    = #10;
  _DELIM_ = ' :;.,+-<>/*%^=()[]|&~@#\`{}'+_TAB_;
  _SPACE_ = ' ';

// Convert string to C-escape string format
function ConvStr(Value: String): String;
var I: Integer;
begin

  Result := '';
  for I := 1 to Length(Value) do begin
    case Value[I] of
      #34: Result := Result + '\' + #34;
      #39: Result := Result + '\' + #39;
      _TAB_: Result := Result + '\t';
      _NL_: begin end;
      _CR_: Result := Result + '\n';
      '\': Result := Result + '\\';
      else Result := Result + Value[I];
    end;
  end;
end;

// Convert string from C-escape string format
function UnconvStr(Value: String): String;
var I: Integer;
begin
  Result := '';
  I := 1;
  while I<=Length(Value) do begin
    if Value[I]='\' then begin
      if I=Length(Value) then break;
      Inc(I);
      case Value[I] of
        'n': Result := Result + _CR_;
        't': Result := Result + _TAB_
        else Result := Result + Value[I];
      end;
    end else Result := Result + Value[I];
    Inc(I);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text:=ConvStr(Memo1.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Memo2.Text:=UnconvStr(Memo1.Text);
end;

相关阅读 >>

Delphi android 安卓系统 关闭打开(显示隐藏)输入法

Delphi md5加密base64加解密

Delphi获取操作系统已运行的所有窗口程序

Delphi中如何设置更改编译生成的exe文件的保存位置

Delphi 控制台读写

Delphi spcomm串口控件的例程

Delphi 在 webservice 中采用 tsoapattachment 传输文件

Delphi 数字分隔

Delphi正则表达式

Delphi setwindowshookex - 设置钩子 unhookwindowshookex - 卸掉钩子

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...