welcome to Qijie's Blog 薛其杰
代码
        static int ReturnIndex(string inputStr, string subStr)
        {
            
if(string.IsNullOrEmpty(inputStr))
                
throw new ArgumentNullException("inputStr is illegal");
            
if(string.IsNullOrEmpty(subStr))
                
throw new ArgumentNullException("subStr is illegal");
            
            
int index=0;
            
int subIndex=0;
            
for(; index <= inputStr.Length - subStr.Length ;index ++)
            {
                
int currentIndex=index;
                
while(subIndex < subStr.Length && inputStr[index++]== subStr[subIndex])
                    subIndex 
++ ;

                
if(subIndex == subStr.Length)
                    
return currentIndex ;

                index
=currentIndex;
                subIndex
=0;
            }
            
return -1;
        }

测试用例:

 

inputStr subStr
""   "123"
"123"   ""

null    "123"
"123"   null

"abc"   "abc"

"abc"   "c"
"abc"   "cd"

"1.2"   "."
"1.2."   "."
" bca debcad"   "bcad"
"bca"   " bc"
"bc ab cd bdab"   "ab"

 

posted on 2010-02-08 20:28  零点零一  阅读(411)  评论(0编辑  收藏  举报