zoukankan      html  css  js  c++  java
  • 查找第一次出现的子串位置

    代码
            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"

  • 相关阅读:
    3728 联合权值[NOIP 2014 Day1 T2]
    关于深度优先遍历图的非递归算法的一个讨论
    图的遍历递归和非递归实现【整理自网络】
    CSS 居中布局
    css怎样让背景充满整个屏幕
    关于html,body{height:100%}的解释
    4103:踩方格
    2287 火车站
    平面分割问题
    蜜蜂路线
  • 原文地址:https://www.cnblogs.com/qixue/p/1666083.html
Copyright © 2011-2022 走看看