我如何确定是否有在我的字符串的开始是两个或一个数字? [英] How do I determine if there are two or one numbers at the start of my string?

查看:155
本文介绍了我如何确定是否有在我的字符串的开始是两个或一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能确定什么数字(与任意位数)是一个字符串的开始?

How can I determine what number (with an arbitrary number of digits) is at the start of a string?

一些可能的字符串

1123|http://example.com
2|daas

应返回1123和2

推荐答案

您可以使用LINQ:

string s = "35|...";

int result = int.Parse(new string(s.TakeWhile(char.IsDigit).ToArray()));

或(如果该号码总是跟着一个 | )好醇'字符串操作:

or (if the number is always followed by a |) good ol' string manipulation:

string s = "35|...";

int result = int.Parse(s.Substring(0, s.IndexOf('|')));

这篇关于我如何确定是否有在我的字符串的开始是两个或一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆