C#算法函數:獲得一個字符串中的最年夜長度的數字。本站提示廣大學習愛好者:(C#算法函數:獲得一個字符串中的最年夜長度的數字)文章只能為提供參考,不一定能成為您想要的結果。以下是C#算法函數:獲得一個字符串中的最年夜長度的數字正文
/// <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;
}
以上就是本文的全體內容,願望能給年夜家一個參考,也願望年夜家多多支撐。