function  SearchSubStr(Substr: string; S: string): Integer;Substr为要搜索的子串,S为源串,搜索到子串后则返回在S中的位置索引;唯一需要注意的地方是Substr和S可能是包含汉字的;
如: Substr := 'a串1'; S := '123字符a串111';

解决方案 »

  1.   

    function Pos(Substr: string; S: string): Integer;
      

  2.   

    // Get the Position of a string, starting at the end 
    function LastPos(SearchStr, Str: string): Integer; 
    var  
      i: Integer; 
      TempStr: string; 
    begin 
      Result := Pos(SearchStr, Str); 
      if Result = 0 then Exit; 
      if (Length(Str) > 0) and (Length(SearchStr) > 0) then 
      begin 
        for i := Length(Str) + Length(SearchStr) - 1 downto Result do 
        begin 
          TempStr := Copy(Str, i, Length(Str)); 
          if Pos(SearchStr, TempStr) > 0 then 
          begin 
            Result := i; 
            break; 
          end; 
        end; 
      end; 
    end; function LastPos(SubStr, S: string): Integer; 
    var 
      Found, Len, Pos: integer; 
    begin 
      Pos := Length(S); 
      Len := Length(SubStr); 
      Found := 0; 
      while (Pos > 0) and (Found = 0) do  
      begin 
        if Copy(S, Pos, Len) = SubStr then 
          Found := Pos; 
        Dec(Pos); 
      end; 
      LastPos := Found; 
    end; 
    // Search for the next occurence of a string from a certain Position 
    function NextPos(SearchStr, Str: string; Position: Integer): Integer; 
    begin 
      Delete(Str, 1, Position - 1); 
      Result := Pos(SearchStr, upperCase(Str)); 
      if Result = 0 then Exit; 
      if (Length(Str) > 0) and (Length(SearchStr) > 0) then 
        Result := Result + Position + 1; 
    end; // Get the number of characters from a certain Position to the string to be searched 
    function NextPosRel(SearchStr, Str: string; Position: Integer): Integer; 
    begin 
      Delete(Str, 1, Position - 1); 
      Result := Pos(SearchStr, UpperCase(Str)) - 1; 
    end; // simple replacement for strings 
    function ReplaceStr(Str, SearchStr, ReplaceStr: string): string; 
    begin 
      while Pos(SearchStr, Str) <> 0 do 
      begin 
        Insert(ReplaceStr, Str, Pos(SearchStr, Str)); 
        Delete(Str, Pos(SearchStr, Str), Length(SearchStr)); 
      end; 
      Result := Str; 
    end;
      

  3.   

    Delphi提供了pos函数呀
    function Pos(Substr: string; S: string): Integer;
      

  4.   

    function AnsiIndexStr( AText: string; AValues:string): Integer; 返回字符串AText在字符串数组AValues中的位置
      

  5.   

    edit2.Text :=inttostr(pos('a中a','bca中ad'));
      

  6.   

    上面的代码我看了,不行,pos更不行,因为是有汉字的,所以有些问题我还得在说明白些:
     比如Substr := '我';S :='蔽壹';因为'我'的编码是 ced2,
    '蔽壹'是 b1ce d2bc
    这样就会查到ced2, 返回错误的结果;
      

  7.   

    上面举例子了,pos返回结果是查的到,但这种情况并不是我希望的;
      

  8.   

    关注,但是我认为如果pos不能查汉字的话,是个bug.
      

  9.   

    pos是可以的,但操作时,如果含有汉字我通常先把字符串强制成widestring类型。
      

  10.   

    我怎么看pos的参数是string类型的?不是widestring类型,
    另外怎么强制转?