Jump to content

Removing Non-Alphanumeric Characters from a string?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Enray

Enray

    Newbie

  • Members
  • Pip
  • 1 posts
How would you do it? I've tried making functions that deletes everything that isn't type char but as you may have guessed that didn't work too well ^^

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Well I'm not sure if it's supported, but I'd aim for a regular expression to do this. That would make this rather simple.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Er, maybe something like this?
function StripNonAlpha(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'] then begin
      DestPtr[0] := SrcPtr[0];
      Inc(DestPtr);
    end;
    Inc(SrcPtr);
  end;
  SetLength(Result, DestPtr - PChar(Result));
end;

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Hi Enray, welcome to CodeCall!

If your string is ansistring (ascii chars), then it's very easy to do. Just walk the members of the string and see if each of them falls into this group ['0'..'9', 'a'..'z', 'A'..'Z'] (like Alex had suggested, but I think he missed the numeric part). If not, just delete the member.

It's different story if you want to support unicode string (including UTF-8) , since in unicode alphanumeric members covers huge number.

What have you came up so far?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users