关于网友提出的“ 如何在字符串数组中查找字符串”问题疑问,本网通过在网上对“ 如何在字符串数组中查找字符串”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 如何在字符串数组中查找字符串
描述: 如何在字符串数组中查找字符串
解决方案1: 把careerist() 的稍修改一下,最简单:
s:array of string;
begin
for i:= Low(s) to High(s) do
begin
If pos('字符',s[i-1])>0 Then ShowMessage('找到');
end;
end;
解决方案2: var x:TStrings;
i:integer;
begin
x:=TStringList.Create;//这就是字符串数组
x.add('abc');
x.Add('bcd');
x.Add('cde');
x.Add('def');
for i:=low(x) to high(x) do
begin
if pos('ab',x[i])>0 then//假定ab是要寻找的字符串
showmessage('success')
else showmessage('fail');
end;
解决方案3: 你可以使用TStrings类,
如:
var
sNames:TStrings;
iIndex:integer;
begin
sNames:=TStringList.Create;
sNames.add('张三');
sNames.add('李四');
sNames.add('王五');
iIndex:=sNames.IndexOf('李四');
//如果iIndex>-1 则说明找到,否则就没有找到。
//你也可以简单的这样用sNames[0]...
end;
解决方案4: //寻找与目标字符串最匹配的字符串
function MaxMatchStr(DestStr:String;Strs:array of String):String;
var
I:Integer;
begin
Result:='';
for I:=1 to Length(Strs) do
//如果与目标匹配
if (Pos(Strs[I],DestStr)>0) and
//而且比现在找到的结果更长
(Length(Strs[I])>Length(Result)) then
//替换当前结果
Result:=Strs[I];
end;
以上介绍了“ 如何在字符串数组中查找字符串”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2903466.html