Function 在文本中找到单词后获取整个单词字符串
我在开发此函数时遇到问题,我有此文本Function 在文本中找到单词后获取整个单词字符串,function,delphi,delphi-xe,Function,Delphi,Delphi Xe,我在开发此函数时遇到问题,我有此文本 Testing Function ok US.Cool rwgehtrhjyw54 US_Cool fhknehq is ryhetjuy6u24 gflekhtrhissfhejyw54i 我的职能: function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer; var i : Integer; x:String; begin Result := 0; for
Testing Function
ok
US.Cool
rwgehtrhjyw54 US_Cool
fhknehq is ryhetjuy6u24
gflekhtrhissfhejyw54i
我的职能:
function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer;
var
i : Integer; x:String;
begin
Result := 0;
for i:= 1 to Length(sTheString) do
begin
x := Copy(sTheString,i,Length(sWordToFind));
if X = sWordToFind then
begin
if X.Length > sWordToFind.Length then
begin
Result := 100;
break;
end else
begin
Result := i;
break;
end;
end;
end;
end;
现在,我想让X成为
US。很酷
,但在这里它总是=US
,因为我想检查剑指和X的长度。我花了几次时间在你的想法上,所以我写了下面的代码,但这不是一个从搜索开始的好方法。通过一些研究,您可以找到提供更好性能的内置函数。您可以尝试它将提供一个完整的函数字符串搜索
无论如何,这段代码使用的是空格分隔符,我希望它对您有用:
function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer;
var
i : Integer; x:String;
flag : Boolean;
begin
Result := 0;
i := 1;
flag := False;
while True do
begin
if i > Length(sTheString) then Break;
if not flag then
x := Copy(sTheString,i,Length(sWordToFind))
else
begin
if sTheString[i] = ' ' then Break;
x := x + sTheString[i];
end;
if (X = sWordToFind) then
begin
flag := True;
if (X.Length >= sWordToFind.Length) and
(sTheString[i + Length(sWordToFind)] = ' ') then
break
else
i := i + Length(sWordToFind) -1;
end;
i := i + 1;
end;
Result := Length(x);
end;
澄清之后,这个问题是关于通过字符串中的起始子字符串来获取单词的长度。例如,当有如下字符串时: 当使用以下子字符串为上述字符串执行所需函数时,应得到如下结果:
hknehq→ 0→ 子字符串不在单词的开头
fhknehq→ 7.→ 单词的长度,因为子字符串位于单词的开头
yhetjuy6u24→ 0→ 子字符串不在单词的开头
ryhetjuy6u24→ 12→ 单词的长度,因为子字符串位于单词的开头
如果是这样,我会这样做:
function GetFoundWordLength(const Text, Word: string): Integer;
const
Separators: TSysCharSet = [' '];
var
RetPos: PChar;
begin
Result := 0;
{ get the pointer to the char where the Word was found in Text }
RetPos := StrPos(PChar(Text), PChar(Word));
{ if the Word was found in Text, and it was at the beginning of Text, or the preceding
char is a defined word separator, we're at the beginning of the word; so let's count
this word's length by iterating chars till the end of Text or until we reach defined
separator }
if Assigned(RetPos) and ((RetPos = PChar(Text)) or CharInSet((RetPos - 1)^, Separators)) then
while not CharInSet(RetPos^, [#0] + Separators) do
begin
Inc(Result);
Inc(RetPos);
end;
end;
SearchBuf
()有什么问题?好吧,它的功能很好,但我不知道如何在上面修改它,它会给出True或False,就像我在US中搜索US
一样。Cool
它会返回True,但我想检查US的长度。Cool
在哪里找到US
@维多利亚投票?!我现在有点困惑。所以你想得到这个词的长度(在哪里,字符串像<代码>美国。酷<代码>你认为是一个词)?例如,您将在文本中搜索54
,并希望获得包含搜索字符串的第一个找到的单词的长度(长度rwgehtrhjyw54
)?停止解释注释中的内容,改为编辑问题。我们不应该玩20个问题来找出你在问什么。在问答网站中,有两项工作要做。第一个是你的,清楚地解释你试图解决的问题,并提出一个明确的问题。我们不能做我们的poart(答案),直到你第一次做你的。@Victoria Of curse我知道这是低效的,但正如我提到的,我只是根据问题所有者的想法写这篇文章,请先阅读代码前的描述,你会明白的。我理解这个问题的意思;那有什么问题?要我检查一下你的密码吗?我能做。。首先,停止复制这么多次。。(只是别难过,我可以教你很多东西)我真的会感激任何教我的人。只要说出我可以向你学习的方法,维多利亚。请用你的方法编辑它。有其他的答案是很好的;)
function GetFoundWordLength(const Text, Word: string): Integer;
const
Separators: TSysCharSet = [' '];
var
RetPos: PChar;
begin
Result := 0;
{ get the pointer to the char where the Word was found in Text }
RetPos := StrPos(PChar(Text), PChar(Word));
{ if the Word was found in Text, and it was at the beginning of Text, or the preceding
char is a defined word separator, we're at the beginning of the word; so let's count
this word's length by iterating chars till the end of Text or until we reach defined
separator }
if Assigned(RetPos) and ((RetPos = PChar(Text)) or CharInSet((RetPos - 1)^, Separators)) then
while not CharInSet(RetPos^, [#0] + Separators) do
begin
Inc(Result);
Inc(RetPos);
end;
end;