[英]Reading two integers at the end of text
I am getting values from the form in the following format: 我从表单获取以下格式的值:
text 2234-7755
What i want to do is read first four integers than the last four integer values separately. 我想做的是分别读取前四个整数而不是最后四个整数值。 Please let me know how can i read that.
请让我知道我该怎么读。 Thanks
谢谢
string[] nums = text.Split("-");
int num1 = Convert.ToInt32(nums[0]);
int num2 = Convert.ToInt32(nums[1]);
If you want to be safer, you can use int.TryParse
如果您想更安全,可以使用
int.TryParse
use split function and get the first value. 使用拆分功能并获取第一个值。
A link for the same 相同的链接
var original="2234-7755";
var intArray = original.Split("-");
int part1 = Convert.ToInt32(intArray[0]);
int part2 = Convert.ToInt32(intArray[1]);
You can also use Int32.TryParse method if you want more control. 如果需要更多控制,也可以使用Int32.TryParse方法。
Are you looking for something like this? 您是否正在寻找这样的东西?
string text = "text 2234-7755";
var matches = Regex.Matches(text, @"(\d+)");
if (matches.Count == 2)
{
int value1 = Convert.ToInt32(matches[0].Groups[1].Value);
int value2 = Convert.ToInt32(matches[1].Groups[1].Value);
}
If the number is always at the end, has 8 digits and is separated by -
you don't need regex: 如果数字始终在末尾,则由8位数字隔开,并且用
-
分隔,您无需使用正则表达式:
string number = text.Substring(text.Length - 9);
string[] both = number.Split('-');
int firstNum, secondNum;
if (both.Length == 2
&& int.TryParse(both[0], out firstNum)
&& int.TryParse(both[1], out secondNum))
{
Console.Write("First number is: {0}\r\nSecond number is: {1}", firstNum, secondNum);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.