Get First word that contains numbers

后端 未结 5 681
醉梦人生
醉梦人生 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:52

    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.

提交回复
热议问题