任何人都可以帮助我如何找到包含数字的第一个完整的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]);
Run Code Online (Sandbox Code Playgroud)
结束;
结果:
Saint
Steven
St
6.A
II.f
9
Run Code Online (Sandbox Code Playgroud)
我想得到第一个包含数字的Word.在示例中:'6.A'.
任何人都知道如何?
为避免在单词中拆分字符串:
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;
Run Code Online (Sandbox Code Playgroud)
首先找到一个数字,然后从数字位置提取单词.