case insensitive Pos

前端 未结 9 1519
一个人的身影
一个人的身影 2021-02-02 00:56

Is there any comparable function like Pos that is not case-sensitive in D2010 (unicode)?

I know I can use Pos(AnsiUpperCase(FindString), AnsiUpperCase(SourceString)) but

9条回答
  •  被撕碎了的回忆
    2021-02-02 01:20

    Here's one that I wrote and have been using for years:

    function XPos( const cSubStr, cString :string ) :integer;
    var
      nLen0, nLen1, nCnt, nCnt2 :integer;
      cFirst :Char;
    begin
      nLen0 := Length(cSubStr);
      nLen1 := Length(cString);
    
      if nLen0 > nLen1 then
        begin
          // the substr is longer than the cString
          result := 0;
        end
    
      else if nLen0 = 0 then
        begin
          // null substr not allowed
          result := 0;
        end
    
      else
    
        begin
    
          // the outer loop finds the first matching character....
          cFirst := UpCase( cSubStr[1] );
          result := 0;
    
          for nCnt := 1 to nLen1 - nLen0 + 1 do
            begin
    
              if UpCase( cString[nCnt] ) = cFirst then
                begin
                  // this might be the start of the substring...at least the first
                  // character matches....
                  result := nCnt;
    
                  for nCnt2 := 2 to nLen0 do
                    begin
    
                      if UpCase( cString[nCnt + nCnt2 - 1] ) <> UpCase( cSubStr[nCnt2] ) then
                        begin
                          // failed
                          result := 0;
                          break;
                        end;
    
                    end;
    
                end;
    
    
              if result > 0 then
                break;
            end;
    
    
        end;
    end;
    
    

提交回复
热议问题