What is the fastest way to remove non-alphanumeric characters from a string in Delphi7?

Valid characters: A to Z, a to z, 0 to 9. The smallest amount of code or one function would be best, since the system is time critical in response to input.

+5
source share
4 answers
uses JclStrings;

  S := StrKeepChars('mystring', ['A'..'Z', 'a'..'z', '0'..'9']);
+8
source

If you understand correctly, you can use this function:

function StripNonAlphaNumeric(const AValue: string): string;
var
  SrcPtr, DestPtr: PChar;
begin
  SrcPtr := PChar(AValue);
  SetLength(Result, Length(AValue));
  DestPtr := PChar(Result);
  while SrcPtr <> #0 do begin
    if SrcPtr[0] in ['a'..'z', 'A'..'Z', '0'..'9'] then begin
      DestPtr[0] := SrcPtr[0];
      Inc(DestPtr);
    end;
    Inc(SrcPtr);
  end;
  SetLength(Result, DestPtr - PChar(Result));
end;

This will use PChar for maximum speed (at the expense of less readability).

: gabr DestPtr [0] DestPtr ^: , , . , , -

function ReplaceNewlines(const AValue: string): string;
var
  SrcPtr, DestPtr: PChar;
begin
  SrcPtr := PChar(AValue);
  SetLength(Result, Length(AValue));
  DestPtr := PChar(Result);
  while SrcPtr <> #0 do begin
    if (SrcPtr[0] = #13) and (SrcPtr[1] = #10) then begin
      DestPtr[0] := '\';
      DestPtr[1] := 't';
      Inc(SrcPtr);
      Inc(DestPtr);
    end else
      DestPtr[0] := SrcPtr[0];
    Inc(SrcPtr);
    Inc(DestPtr);
  end;
  SetLength(Result, DestPtr - PChar(Result));
end;

^.

, - , , ?

+10

.

Delphi 7, Delphi 2009, char ( ansichar).

, , :

case key of 
  'A'..'Z', 'a'..'z', '0'..'9' : begin end; // No action
else
  Key := #0;
end;

, :

if not ValidChar(key) then
  Key := #0;

In this case, you can use ValidChar in several places, and if you need to change it, you only need to change it.

+3
source

OnKeypress Event

start if not (enter ['A' .. 'Z', 'a' .. 'z', '0' .. '9']), then Key: = # 0; end;

+1
source

All Articles