德尔福从字符串中删除符号

问题描述 投票:0回答:2

我已经搜索并尝试了,这是为了从一个字符串中删除符号的循环,我需要这个,因为某些位置可以有一个“/”或其他类型的符号,我需要它删除,因为FTP会认为这是一个文件夹,我需要一个名称的字符串。

字符串“地方”从人员变化当前工作位置,例如有一个“地方” =“S / A StorageRoom”,字符串化妆的FTP的“/”部分认为这是一个子文件夹。

现在我用这个,我认为这是相当大的,可能使用“缩短”:

   place := StringReplace(place, ',', '', [rfReplaceAll]);
   place := StringReplace(place, '.', '', [rfReplaceAll]);
   place := StringReplace(place, '/', '', [rfReplaceAll]);
   place := StringReplace(place, '!', '', [rfReplaceAll]);
   place := StringReplace(place, '@', '', [rfReplaceAll]);
   place := StringReplace(place, '#', '', [rfReplaceAll]);
   place := StringReplace(place, '$', '', [rfReplaceAll]);
   place := StringReplace(place, '%', '', [rfReplaceAll]);
   place := StringReplace(place, '^', '', [rfReplaceAll]);
   place := StringReplace(place, '&', '', [rfReplaceAll]);
   place := StringReplace(place, '*', '', [rfReplaceAll]);
   place := StringReplace(place, '''', '', [rfReplaceAll]);
   place := StringReplace(place, '"', '', [rfReplaceAll]);
   place := StringReplace(place, ';', '', [rfReplaceAll]);
   place := StringReplace(place, '_', '', [rfReplaceAll]);
   place := StringReplace(place, '(', '', [rfReplaceAll]);
   place := StringReplace(place, ')', '', [rfReplaceAll]);
   place := StringReplace(place, ':', '', [rfReplaceAll]);
   place := StringReplace(place, '|', '', [rfReplaceAll]);
   place := StringReplace(place, '[', '', [rfReplaceAll]);
   place := StringReplace(place, ']', '', [rfReplaceAll]);
   place := StringReplace(place, '\', '', [rfReplaceAll]);

编辑:我目前使用的RAD Studio 10.1柏林

delphi delphi-10.1-berlin
2个回答
3
投票

在寻求避免不必要的堆分配,这里是@ Ancaron的回答是只做一次分配,以产生答案最终的尺寸减小的变体。

诀窍是在开始预分配结果字符串,然后接受字符填充它。在结束时,调节所得字符串长度。

program TestStripChars;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function StripChars (const Text : string; const InValidChars : SysUtils.TSysCharSet) : string;
var
  i,j : Integer;
begin
  SetLength(Result,Length(Text));  // Preallocate result maximum length
  j := 0;  // Resulting string length counter
  for i := 1 to Length(Text) do begin
    if not CharInSet(Text[i],InValidChars) then begin
      Inc(j);
      Result[j] := Text[i];
    end;
  end;
  SetLength(Result,j); // Set result actual length
end;

var
  place : String;
begin
  place := 'Hell$$o D.,e.$lphi';
  place := StripChars(place,[',','.','$']);
  WriteLn(place);
  ReadLn;
end.

从评论它看起来像OP使用具有零基于字符串处理一个编译器设置。

这里是处理这两种情况下的函数:

function StripChars ( const Text : string; const InValidChars : SysUtils.TSysCharSet) : string;
var
  i,j,zbsAdj : Integer;
begin
  SetLength(Result,Length(Text));  // Preallocate result maximum length
  j := 0; // Resulting string length counter
  zbsAdj := 1-Low(String); // Handles zero based string offset
  for i := Low(Text) to High(Text) do begin
    if not CharInSet(Text[i],InValidChars) then begin
      Inc(j);
      Result[j-zbsAdj] := Text[i];
    end;
  end;
  SetLength(Result,j); // Set result actual length
end;

0
投票

你冷只是使用像函数

function StripChars ( const Text : string; InValidChars : TSetOfChar ) : string;
var
  S : string;
  i : integer;
begin
  Result := '';
  if Length(Text) > 0 then
  begin
    S := '';
    for i := 1 to length ( Text ) do
      if not CharInSet(Text[i],InValidChars) then  
        S := S + Text [ i ];
    Result := S;
  end;
end;

然后只调用

place := StripChars(place,[',','.', and so on]);
© www.soinside.com 2019 - 2024. All rights reserved.