获取包含数字的第一个单词

时间:2015-10-05 11:53:23

标签: delphi delphi-xe7

任何人都可以帮忙我如何找到第一个包含数字的完整Word?我有一个地址,例如:

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

端;

结果:

Saint
Steven
St
6.A
II.f
9

我想获得包含数字的第一个Word。在示例中:&#39; 6.A&#39;。

任何人都知道如何?

5 个答案:

答案 0 :(得分:6)

避免在单词中拆分字符串:

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;

首先找到一个数字,然后从数字位置提取单词。

答案 1 :(得分:4)

通过循环字符串并检查每个字符来测试字符串是否包含数字。例如:

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;

或者您可能更喜欢使用System.Character单元中的记录帮助程序方法编写if语句。

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 :(得分:4)

您可以使用Regular Expressions完成此任务:

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.

以上版画:

  

6.A
9

要获得包含数字的第一个单词,例如您的问题,您可以考虑在代码中添加一个函数:

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;

可以像这样调用新添加的函数:

showmessage(GetWordContainingNumber(SourceString));

答案 3 :(得分:1)

你可以使用这样的simething来知道一个单词是否包含字符'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;

答案 4 :(得分:0)

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;