字符串过滤问题各位先生。请问我如何判断一字符串是否包括指定的字符,
我需要过滤的字符有多个,用分号分隔,如“钢;电;管;热”
只要我在一长字符串查到其中的一个,如钢,条件就为真,查到包含电字条件也为真,....

解决方案 »

  1.   

    If pos(“钢;电;管;热”, string)>0 then 
      

  2.   

    if pos('钢', '钢;电;管;热') > 0 then
      条件为真
      

  3.   

    If pos(“钢”, string)>0 or pos(“电”, string)>0 or pos(“管”, string)>0  or pos(“热”, string)>0 then
      

  4.   

    {
    如果要查找的字符串是不确定的,用下面的函数,其中subStr是你要查找的字符串如“钢;电;管;热”,Source是被查找的字符串,如果在Source中找到SubStr则函数返回True,否则为False
    }
      function SearchStrings(subStr, Source: String): Boolean;
      var
        sl: TStrings;
        i: Integer;
      begin
        Result := False;
        sl := TStringList.Create;
        try
          sl.Delimiter := ';';
          sl.DelimitedText := Trim(subStr);
          for i := 0 to (sl.Count - 1) do
            if (Pos(sl.Strings[i], Source) > 0) then
            begin
              Result := True;
              Break;
            end;
        finally
          sl.Free;
        end;
      end;
      

  5.   

    filter:string;  //用于存放要过滤的字符串
    mystr:string;  //目标字符串
    i,j:integer;
    for i=0 to length(mystr) do
    begin
      for j=0 to length(filter) do
      begin
        if filter[j]=';' then
        begin
          j:=j+1;
        end;
        if mystr[i]=filter[j] then
        begin
          ShowMessage('find');
        end;
      end;
    end;