Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Removing Non-Alphanumeric Characters from a string?


  • Please log in to reply
3 replies to this topic

#1 Enray


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 13 February 2011 - 03:48 PM

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 ^^
  • 0

#2 BlaineSch


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 13 February 2011 - 04:30 PM

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.
  • 0

#3 Alexander



  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 13 February 2011 - 07:28 PM

Er, maybe something like this?
function StripNonAlpha(const AValue: string): string;
  SrcPtr, DestPtr: PChar;
  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];
  SetLength(Result, DestPtr - PChar(Result));

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.

#4 Luthfi


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 14 February 2011 - 12:40 AM

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?
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download