C#算法函數(shù):獲取一個字符串中的最大長度的數(shù)字
更新時間:2016年06月16日 10:08:21 作者:Robin
這篇文章介紹了使用C#獲取一個字符串中最大長度的數(shù)字的實例代碼,有需要的朋友可以參考一下。
/// <summary>
/// 獲取字符串最長的數(shù)字
/// </summary>
/// <param name="inputStr">輸入字符串</param>
/// <returns>最長數(shù)字</returns>
public string GetMaxLenNumber(string inputStr)
{
//將字符串中的字符存放到數(shù)組中,便于處理
char[] strCharArray = inputStr.ToCharArray();
//開始處理的位置
int startPos = 0;
//當(dāng)前處理的字符長度
int tempCharCount = 0;
//數(shù)字的最長長度
int maxLen = 0;
//數(shù)組的總長度
int len = strCharArray.Length;
int pos = 0;
while (startPos < len)
{
//循環(huán)中的臨時最大長度
int tempMax = 0;
while (tempCharCount + startPos < len)
{
//開始處理的字符
char c = strCharArray[tempCharCount + startPos];
if (char.IsNumber(c))
{
//如果是數(shù)字
tempMax++;
if (tempMax > maxLen)
{
maxLen = tempMax;
pos = startPos;
}
}
else
{
//不是數(shù)字
tempMax = 0;
startPos++;
break;
}
tempCharCount++;
}
if (startPos + tempCharCount == len)
{
break;
}
tempCharCount = 0;
}
string s = inputStr.Substring(pos, maxLen);
return s;
}
以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)
這篇文章介紹了C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01

