如何检查TStringList是否包含特定符号?
如果在字符串列表中找到以下任何内容,我希望显示一条消息:
\ /:*?“<>\x{e76f}
类似于:
var
SL: TStringList;
i: Integer;
begin
SL := TStringList.Create;
try
for i := 0 to SL.Count -1 do
begin
if SL.Strings[i] ?? then
begin
MessageDlg('Stringlist contains bad characters', mtError, [mbOK], 0);
end else
begin
// no bad characters
end;
end;
finally
SL.Free;
end;
end;发布于 2011-12-26 01:28:21
您可以使用pos()函数来测试字符串是否包含特定字符。SL.Text是一个字符串中的所有字符串。
if (Pos('\', SL.Text) > 0) OR (pos('/', SL.Text) > 0) then
MessageDlg('Stringlist contains bad characters', mtError, [mbOK], 0);等。
发布于 2011-12-26 01:41:11
使用pos()函数搜索TStringList.Text属性中的每个符号。或者,插入TStringList.Text属性中的字符,并检查它们是否位于一组不良字符中。第二种方法可能更快:
Function Tsomething.CheckList(list:TStringList;badChars:set of char):boolean;
var charIndex:integer;
thisChar:char;
begin
result:=false; // in case of empty list
for charIndex:=1 to length(list.text) do // indices into a string start at 1
begin
thisChar:=list.text[charIndex];
result:=thisChar in badChars;
if result then exit;
end;
end;
..
if CheckList(myList,['\','/',':','*','?','"','<','>','|']) then application.messageBox(blah);..but,您必须同时尝试确保这一点。
发布于 2011-12-26 01:25:49
const
ForbiddenChars = ['\','/',':','*','?','"','<','>','|'];
var
SL: TStringList;
StrTemp: String;
i,j: Integer;
begin
SL := TStringList.Create;
try
for i := 0 to SL.Count - 1 do
begin
StrTemp := SL.Strings[i];
for j := 1 to Length(StrTemp) do
begin
if StrTemp[j] in ForbiddenChars then
begin
MessageDlg('Stringlist contains bad characters', mtError, [mbOK], 0);
end;
end;
end;
finally
SL.Free;
end;
end;如果您使用免费Pascal,也可以使用更简单的StrUtils.PosSet(ForbiddenChars,SL.Strings[i]),但我不知道这个函数是否也存在于Delphi中。
https://stackoverflow.com/questions/8632437
复制相似问题