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"

  • 相关阅读:
    很久以前写的一个功能测试用例
    中外白领和无领的一天
    Five bugs in five minutes...
    Oracle SQL 性能优化技巧
    10款常用Java测试工具[转载]
    AJAX、AJAX实例及AJAX源代码(asp)
    各大银行的暗示[笑话]
    Tomcat集群与负载均衡(转载)
    简述CMMI2级的7个PA
    全面介绍单元测试 -转贴
  • 原文地址:https://www.cnblogs.com/qixue/p/1666083.html
Copyright © 2011-2022 走看看