Get First word that contains numbers

孤街浪徒 提交于 2019-12-02 03:14:21

问题


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?


回答1:


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;



回答2:


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.




回答3:


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



回答4:


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;



回答5:


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;    


来源:https://stackoverflow.com/questions/32948001/get-first-word-that-contains-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!