Get First word that contains numbers

后端 未结 5 683
醉梦人生
醉梦人生 2021-01-22 18:58

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         


        
5条回答
  •  无人共我
    2021-01-22 19:43

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

提交回复
热议问题