gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:01:34 24 4
gpt4 key购买 nike

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

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”。

有人知道怎么做吗?

最佳答案

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

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;

先定位一个数字,然后从该数字位置提取单词。

关于delphi - 获取包含数字的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com