C#算法函数:获取一个字符串中的最大长度的数字
作者:Robin 时间:2022-12-25 10:20:04
/// <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;
}
标签:C#,算法,字符串,最大长度的数字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中的ArrayList容量及扩容方式
2023-10-17 17:24:40
Android使用GestureOverlayView控件实现手势识别
2023-03-02 08:46:26
![](https://img.aspxhome.com/file/2023/6/88096_0s.png)
C#调用存储过程详解(带返回值、参数输入输出等)
2023-12-06 01:06:35
struts2简介_动力节点Java学院整理
2023-06-24 02:04:07
![](https://img.aspxhome.com/file/2023/3/72323_0s.jpg)
java实现二叉树的创建及5种遍历方法(总结)
2022-03-14 09:00:28
Java动态规划之编辑距离问题示例代码
2023-10-30 00:21:21
![](https://img.aspxhome.com/file/2023/1/84781_0s.gif)
java模拟微信抢红包的实例代码
2021-12-08 19:51:21
SpringBoot-RestTemplate实现调用第三方API的方式
2022-12-29 09:49:56
java信号量控制线程打印顺序的示例分享
2023-05-09 12:27:38
详解Java的Hibernate框架中的Interceptor和Collection
2023-08-18 04:02:55
![](https://img.aspxhome.com/file/2023/2/72572_0s.png)
Spring Security权限管理实现接口动态权限控制
2022-07-03 12:25:53
![](https://img.aspxhome.com/file/2023/5/83445_0s.png)
Unity实现简单手势识别
2022-10-14 07:52:05
Java 中String StringBuilder 与 StringBuffer详解及用法实例
2021-06-17 12:25:32
浅析Spring Boot中的spring-boot-load模块
2023-11-23 02:39:31
![](https://img.aspxhome.com/file/2023/6/58756_0s.png)
关于Java双大括号{{}}的具体使用
2021-05-28 16:32:01
spring的jdbctemplate的crud的基类dao
2022-05-30 13:05:17
Spring boot配置绑定和配置属性校验的方式详解
2022-04-21 03:06:06
![](https://img.aspxhome.com/file/2023/4/64884_0s.png)
JPA save()方法将字段更新为null的解决方案
2023-10-28 22:29:28
Spring注解配置IOC,DI的方法详解
2023-10-18 14:06:23
![](https://img.aspxhome.com/file/2023/1/77131_0s.png)
在C#程序中对MessageBox进行定位的方法
2022-03-04 18:45:26