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"

  • 相关阅读:
    Tomcat服务器原理详解
    junit入门
    lombok
    java 运行指定类的main函数
    席位分配问题——惯例Q值法和d&#39;hondt法的MATLAB程序
    5.2 calendar--通用日期的相关函数(3)
    [笔记]软件体系结构(1)--模式初印象
    hdu 1003
    linux虚拟机上挂载U盘
    Android中Java与web通信
  • 原文地址:https://www.cnblogs.com/qixue/p/1666083.html
Copyright © 2011-2022 走看看