How to remove characters from a string before the first char - is this a letter?

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

If the program reads "123% ^ & abc", then the result should be "abc" in the ASCII table letters from 65..90 and from 97..122

Here is how much 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 do not understand why this is not working? Can you help me with this little procedure? Thanks.

+4
source share
2 answers

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 complex methods that also work with Unicode Delphi, see my answer to a similar question . [This removes all non-alpha characters from the string.]

So why is your algorithm not working? Well, that should work, and it works for me. But note that it can be written in a 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 source code is that it will fail if s is an empty string. In fact, then s[1] does not exist. It will not work if s consists entirely of non-alpha characters (for example, '!"#ยค% ).

+13
source

Although the previous solutions do work, they are very inefficient. For two reasons: 1. Searching in a set takes a lot of time 2. Deleting each time a character from a string is even more inefficient, since the string (object) must delete the character inside and configure its array, etc.

Ideally, you enter your string in PChar and work with it when checking the char -range manually. We will continue the search until we find the first letter, and only then will we call the DeleteString method. Here is a demonstration of 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; 
+2
source

Source: https://habr.com/ru/post/1342498/


All Articles