Anyone can help with how can I find the first full Word that contains numbers? I have an adress, for example:
procedure TForm1.Button4Click(Sender: TObject);
var
Test if a string contains a digit by looping of the string and checking each character. For instance:
function ContainsDigit(const S: string): Boolean;
var
C: Char;
begin
for C in S do
if (C >= '0') and (C <= '9') then
exit(True);
exit(False);
end;
Or you might prefer to write the if statement using the record helper methods from the System.Character
unit.
uses
System.Character;
....
function ContainsDigit(const S: string): Boolean;
var
C: Char;
begin
for C in S do
if C.IsDigit then
exit(True);
exit(False);
end;
You can use simething like this to know if a word contains chars '0'..'9'
function DigitInWord(s: string): boolean;
var
ch: char;
begin
result := false;
for ch :='0' to '9' do
if Pos(s, ch) > 0 then
begin
result := true;
break;
end;
end;
You can use Regular Expressions to accomplish this task:
const
CWORDS = 'Saint Steven St 6.A II.f 9';
CPATTERN = '([a-zA-z\.]*[0-9]+[a-zA-z\.]*)+';
var
re: TRegEx;
match: TMatch;
begin
re := TRegEx.Create(CPATTERN);
match := re.Match(CWORDS);
while match.Success do begin
WriteLn(match.Value);
match := match.NextMatch;
end;
end.
The above prints:
6.A
9
To get the very first word containing numbers, like your question requires, you may consider to add a function to your code:
function GetWordContainingNumber(const AValue: string): string;
const
CPATTERN = . . .;//what the hell the pattern is
var
re: TRegEx;
match: TMatch;
begin
re := TRegEx.Create(CPATTERN);
match := re.Match(AValue);
if match.Success then
Result := match.Value
else
Result := '';
end;
The newly added function can be called like this:
showmessage(GetWordContainingNumber(SourceString));
function WordContainsNumber(const AWord: string): boolean;
var
i: integer;
begin
for i:=1 to Length(AWord) do
if CharInSet(AWord[i], ['0'..'9']) then
Exit(true);
Exit(false);
end;
function GetFirstWordThatContainsANumber(const AWords: TArray<string>): string;
var
CurrentWord: string;
begin
Result := '';
for CurrentWord in AWords do
if WordContainsNumber(CurrentWord) then
Exit(CurrentWord);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
SourceString : String;
strArray : TArray<string>;
i : Integer;
begin
SourceString := 'Saint Steven St 6.A II.f 9';
strArray := SourceString.Split([' ']);
for i := 0 to Length(strArray)-1 do
showmessage(strArray[i]);
ShowMessage('The first word containing a number is ' + GetFirstWordThatContainsANumber(strArray));
end;
To avoid splitting the string in words:
function ExtractFirstWordWithNumber(const SourceString: String): String;
var
i,start,stop: Integer;
begin
for i := 1 to Length(SourceString) do
begin
if TCharacter.IsDigit(SourceString[i]) then
begin // a digit is found, now get start location of word
start := i;
while (start > 1) and
(not TCharacter.IsWhiteSpace(SourceString[start-1])) do
Dec(start);
// locate stop position of word
stop := i;
while (stop < Length(SourceString)) and
(not TCharacter.IsWhiteSpace(SourceString[stop+1])) do
Inc(stop);
// Finally extract the word with a number
Exit(Copy(SourceString,start,stop-start+1));
end;
end;
Result := '';
end;
First locate a digit, then extract the word from the digit position.