I am trying to delete every character from the beginning of my string, that is not an Alpha-character.
However, when there are only non-alpha characters (like “!!” or “?!?”) in the string, it spits out an Access Violation!
Here is my code:
// The Log(); is a routine that adds stuff to my log memo.
Log('Begin Parse');
while not IsLetter(ParsedName[1]) do
begin
Log('Checking Length - Length is '+IntToStr(Length(ParsedName))+' ...');
if Length(ParsedName) <> 0 then
Begin
Log('Deleting Char ...');
Delete(ParsedName,1,1);
Log('Deleted Char ...');
End;
Log('Checking Length - Length is now '+IntToStr(Length(ParsedName))+' ...');
end;
// It never reaches this point!
Log('End Parse');
This is what my log produces:
21:51:19: Checking Length - Length is 2 ...
21:51:19: Deleting Char ...
21:51:19: Deleted Char ...
21:51:19: Checking Length - Length is now 1 ...
21:51:19: Checking Length - Length is 1 ...
21:51:19: Deleting Char ...
21:51:19: Deleted Char ...
21:51:19: Checking Length - Length is now 0 ...
21:51:19: Access violation at address 007A1C09 in module 'Project1.exe'. Read of address 00000000
As you see, it happens right after all the chars have been deleted. I assume the problem lies that somehow, I am trying to access something that is not there, but how I am doing that, I cannot see.
EDIT: Yes, I know it’s a stupid question and all that stuff – I just oversaw something. Don’t tell me that doesen’t ever happen to you 😉
This question has nothing to do with
Delete. Delete works even if you tell it to delete characters that do not exist.The line
tries to access
ParsedName[1], so this character has better to exist. Your code isn’t particularly beautiful, but a simple workaround isYou can do just