C#算法函數(shù):獲取一個(gè)字符串中的最大長(zhǎng)度的數(shù)字
更新時(shí)間:2016年06月16日 10:08:21 作者:Robin
這篇文章介紹了使用C#獲取一個(gè)字符串中最大長(zhǎng)度的數(shù)字的實(shí)例代碼,有需要的朋友可以參考一下。
/// <summary>
/// 獲取字符串最長(zhǎng)的數(shù)字
/// </summary>
/// <param name="inputStr">輸入字符串</param>
/// <returns>最長(zhǎng)數(shù)字</returns>
public string GetMaxLenNumber(string inputStr)
{
//將字符串中的字符存放到數(shù)組中,便于處理
char[] strCharArray = inputStr.ToCharArray();
//開始處理的位置
int startPos = 0;
//當(dāng)前處理的字符長(zhǎng)度
int tempCharCount = 0;
//數(shù)字的最長(zhǎng)長(zhǎng)度
int maxLen = 0;
//數(shù)組的總長(zhǎng)度
int len = strCharArray.Length;
int pos = 0;
while (startPos < len)
{
//循環(huán)中的臨時(shí)最大長(zhǎng)度
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;
}
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
c#圖片縮放圖片剪切功能實(shí)現(xiàn)(等比縮放)
c#圖片縮放剪切功能實(shí)現(xiàn),代碼中包含了c#圖片處理的一些基礎(chǔ)知識(shí),與大家分享2013-12-12
C# BeginInvoke實(shí)現(xiàn)異步編程方式
這篇文章主要介紹了C# BeginInvoke實(shí)現(xiàn)異步編程方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
C#循環(huán)與循環(huán)控制的表達(dá)式樹實(shí)現(xiàn)
這篇文章介紹了C#循環(huán)與循環(huán)控制的表達(dá)式樹實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01

