从字符串中获取多个数字

编程入门 行业动态 更新时间:2024-10-28 06:27:22
本文介绍了从字符串中获取多个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有像

AS_!SD 2453iur ks@d9304-52kasd

AS_!SD 2453iur ks@d9304-52kasd

我需要获取字符串的 2 个第一个数字:

I need to get the 2 frist numbres of the string:

对于这种情况将是:2453 和 9304

for that case will be: 2453 and 9304

我在字符串中没有任何分隔符来尝试拆分,并且数字和字符串的长度是可变的,我在 WPF 中使用 C# 框架 4.0.

I don't have any delimiter in the string to try a split, and the length of the numbers and string is variable, I'm working in C# framework 4.0 in a WPF.

感谢您的帮助,并为我的英语不好

thanks for the help, and sorry for my bad english

推荐答案

此解决方案将取两个前数字,每个数字可以有任意位数

This solution will take two first numbers, each can have any number of digits

string s = "AS_!SD 2453iur ks@d9304-52kasd"; MatchCollection matches = Regex.Matches(s, @"\d+"); string[] result = matches.Cast<Match>() .Take(2) .Select(match => match.Value) .ToArray(); Console.WriteLine( string.Join(Environment.NewLine, result) );

将打印

2453 9304

您可以通过 result.Select(int.Parse).ToArray();

更多推荐

从字符串中获取多个数字

本文发布于:2023-11-06 04:34:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!