获取包含数字的第一个单词
Get First word that contains numbers
任何人都可以帮助我如何找到第一个包含数字的完整单词?我有一个地址,例如:
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
我想获取第一个包含数字的单词。在示例中:'6.A'.
有人知道怎么做吗?
您可以使用类似这样的方法来了解单词是否包含字符 '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;
通过循环字符串并检查每个字符来测试字符串是否包含数字。例如:
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;
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;
您可以使用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));
为了避免在单词中拆分字符串:
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;
先定位一个数字,然后从数字位置提取单词。
任何人都可以帮助我如何找到第一个包含数字的完整单词?我有一个地址,例如:
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
我想获取第一个包含数字的单词。在示例中:'6.A'.
有人知道怎么做吗?
您可以使用类似这样的方法来了解单词是否包含字符 '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;
通过循环字符串并检查每个字符来测试字符串是否包含数字。例如:
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;
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;
您可以使用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));
为了避免在单词中拆分字符串:
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;
先定位一个数字,然后从数字位置提取单词。