zoukankan      html  css  js  c++  java
  • 算法函数:得到一个字符串中的最大长度的数字

     /// <summary>
            
    /// 获取字符串最长的数字
            
    /// </summary>
            
    /// <param name="inputStr">输入字符串</param>
            
    /// <returns>最长数字</returns>

            public string GetMaxLenNumber(string inputStr)
            
    {
                
    //将字符串中的字符存放到数组中,便于处理
                char[] strCharArray = inputStr.ToCharArray();
                
    //开始处理的位置
                int startPos = 0;
                
    //当前处理的字符长度
                int tempCharCount = 0;
                
    //数字的最长长度
                int maxLen = 0;
                
    //数组的总长度
                int len = strCharArray.Length;
                
    int pos = 0;
                
    while (startPos < len)
                
    {
                    
    //循环中的临时最大长度
                    int tempMax = 0;
                    
    while (tempCharCount + startPos < len)
                    
    {
                        
    //开始处理的字符
                        char c = strCharArray[tempCharCount + startPos];
                        
    if (char.IsNumber(c))
                        
    {
                            
    //如果是数字
                            tempMax++;
                            
    if (tempMax > maxLen)
                            
    {
                                maxLen 
    = tempMax;
                                pos 
    = startPos;
                            }
                           
                        }

                        
    else
                        
    {
                            
    //不是数字
                            tempMax = 0;
                            startPos
    ++;
                            
    break;
                        }

                        tempCharCount
    ++;
                    }

                    
    if (startPos + tempCharCount == len)
                    
    {
                        
    break;
                    }

                    tempCharCount 
    = 0;             
                }

                
    string s = inputStr.Substring(pos, maxLen);
                
    return s;
            }
          
  • 相关阅读:
    python中列表排序的方法
    pyrhon3中字符串方法
    python数据探索与数据与清洗概述
    2020年日期表-python实现
    python中字符串离散化的例子
    python中常见的日期处理方法
    如何简单地理解Python中的if __name__ == '__main__'
    我的老爸老了
    关于
    关于
  • 原文地址:https://www.cnblogs.com/jillzhang/p/881148.html
Copyright © 2011-2022 走看看