
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;
}
{
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"