开发者

How to delete chars from string until first char is a letter?

I have a program which works with strings (Pascal). After reading a string if the first char is not a letter then I need to delete all first characters until the first is a letter. I have tried to write it several times, but always it deletes all string or nothing.

If program reads "123%^&abc" then result should be "abc" In A开发者_如何学CSCII table letters are from 65..90 and from 97..122

This is how far I am:

variables    a: set of 65..90;
             b: set of 97..122;
-------------------
  bool:=false;
  While (bool=false) do
  begin
    Writeln(s[1]);
    If (Ord(s[1]) in a) or (Ord(s[1]) in b) then
    begin
    bool:=true;
    end else
    delete(s,1,1);
  end;

I don't understand why it does not work? Can you help me with this little procedure? Thank you.


You could do

function RemoveNonAlphaASCIIFromStart(const Str: AnsiString): AnsiString;
const
  ALPHA = ['A'..'Z', 'a'..'z'];
var
  i: Integer;
  firstIndex: integer;
begin
  result := '';
  firstIndex := 0;
  for i := 1 to length(Str) do
    if Str[i] in ALPHA then
    begin
      firstIndex := i;
      break;
    end;
  if firstIndex > 0 then
    result := Copy(Str, firstIndex, length(Str));
end;

or, as a procedure

procedure RemoveNonAlphaASCIIFromStart(var Str: AnsiString);
const
  ALPHA = ['A'..'Z', 'a'..'z'];
var
  i: Integer;
  firstIndex: integer;
begin
  firstIndex := 0;
  for i := 1 to length(Str) do
    if Str[i] in ALPHA then
    begin
      firstIndex := i;
      break;
    end;
  if firstIndex > 0 then
    Delete(Str, 1, firstIndex - 1)
  else
    Str := '';
end;

For more sophisticated methods, that also work with Unicode Delphi, see my answer to a similar question. [This removes all non-alpha chars from the string.]

So, why doesn't your algorithm work? Well, it should work, and it works for me. But notice that it can be written in the slightly more elegant form

const
  ALPHA = ['A'..'Z', 'a'..'z'];

while true do
  if (length(s) = 0) or (s[1] in ALPHA) then
    break
  else
    delete(s, 1, 1);

One problem, however, with the OP's original code is that it will fail if s is the empty string. Indeed, then s[1] doesn't exist. It won't work either if s consists entirely of non-alpha characters (e.g. '!"#¤%).


Allthough the previous solutions do work, they are highly ineffitient. Because of 2 reasons: 1. Searching in a set is time consuming 2. Deleting every time a character out of a string is even more ineffitient, as the string (object) has to remove the character internally and adjust its array, etc.

Ideally you cast your string into PChar and work with that, while checking the char-range "manually". We'll let the search run until we find the first letter and only then we call the DeleteString method. Here's a demo on my approach:

procedure Frapp;
var
  TheString: string;
  pcStr: PChar;
  StrLen, I: Integer;
begin
  TheString := '123%^&abc';
  StrLen := Length(TheString);
  pcStr := PChar(TheString);

  for I := 0 to StrLen - 1 do
  begin
    if ((pcStr^ >= #65) and (pcStr <= #90)) or ((pcStr >= #97) and (pcStr <= #122)) then
    begin
      Delete(TheString, 1, I);
      Break;
    end;
    Inc(pcStr);
  end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜