Steve88
Steve88

Reputation: 2416

Get First word that contains numbers

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
  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]);

end;

Result:

Saint
Steven
St
6.A
II.f
9

I want to get the first Word that contain number. In the example: '6.A'.

Anyone have an idea how?

Upvotes: 2

Views: 1233

Answers (5)

LU RD
LU RD

Reputation: 34909

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.

Upvotes: 6

fantaghirocco
fantaghirocco

Reputation: 4878

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));

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 613163

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;

Upvotes: 4

Wosi
Wosi

Reputation: 45283

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;    

Upvotes: 0

asd-tm
asd-tm

Reputation: 5263

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;

Upvotes: 1

Related Questions