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"

  • 相关阅读:
    flask + supervisor + gunicorn
    sqlacodegen 的使用
    centos 安装 postgresql
    zipfile 压缩文件
    jmeter跨线程调用和线程内调用
    获取时间
    ant+jmeter环境配置
    Jenkins环境部署
    jmeter分布式性能测试部署
    怎么在Windows Server服务器上发布jsp网站
  • 原文地址:https://www.cnblogs.com/qixue/p/1666083.html
Copyright © 2011-2022 走看看