delphi - strip out all non standard text characers from string

前端 未结 6 1395
-上瘾入骨i
-上瘾入骨i 2021-02-13 14:52

I need to strip out all non standard text characers from a string. I need remove all non ascii and control characters (except line feeds/carriage returns).

6条回答
  •  独厮守ぢ
    2021-02-13 15:39

    my version with Result array of byte :

    interface

    type
      TSBox = array of byte;
    

    and the function :

    function StripNonAscii(buf: array of byte): TSBox;
    var temp: TSBox;
        countr, countr2: integer;
    const validchars : TSysCharSet = [#32..#127];
    begin
    if Length(buf) = 0 then exit;
    countr2:= 0;
    SetLength(temp, Length(buf)); //setze temp auf länge buff
    for countr := 0 to Length(buf) do if CharInSet(chr(buf[countr]), validchars) then
      begin
        temp[countr2] := buf[countr];
        inc(countr2); //count valid chars
      end;
    SetLength(temp, countr2);
    Result := temp;
    end;
    

提交回复
热议问题