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"

  • 相关阅读:
    10 个迅速提升你 Git 水平的提示
    GitLab-CI与GitLab-Runner
    WEB应用安全解决方案测试验证
    sparse representation 与sparse coding 的区别的观点
    The Ph.D. Grind
    Potential Pythonic Pitfalls
    Overfitting & Regularization
    LVM的一般操作过程
    你跟大牛之间仅仅差一个google
    Hadoop伪分布式模式部署
  • 原文地址:https://www.cnblogs.com/qixue/p/1666083.html
Copyright © 2011-2022 走看看