Simba Code:
function GetMonsterName(text: String): String;
begin
result := Copy(text, Pos(' ', text) + 1, Length(text) - Pos(' ', text) + 1);
end;
Code:
WriteLn(GetMonsterName('40 Hellhounds'));
Outputs: Hellhounds
WriteLn(GetMonsterName('40 Iron Dragons'));
Outputs: Iron Dragons
Best practice would include sanity checks on input, but then it wouldn't be a pretty one-liner. Something like this:
Simba Code:
function GetMonsterName(text: String): String;
begin
if Pos(' ', text) = 0 then
RaiseException(erCustomError, 'GetMonsterName(): invalid string passed to function');
result := Copy(text, Pos(' ', text) + 1, Length(text) - Pos(' ', text) + 1);
end;
You could then catch the exception and handle it, such as:
Simba Code:
begin
try
WriteLn(GetMonsterName('fasdfasegasrdg'));
except
WriteLn('lorem ipsum'); //handle your exception here, maybe try to re-fetch it?
end;
end.
Or just let it error out.