本文介绍了使用parse方法将字符串转换为int。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个等于xxxxx-xxxxx-xxxxx-xxxxx的字符串(其中'x'代表一个数字)
我想分配这个输入的字符串到一个名为installationNumber的变量;我知道它必须是一个字符串,因为它包含每5组int之间的破折号。如何使用解析方法将此字符串转换为int的正确方法,以便我可以使用数学算法?
干杯
I have a string that is equal to xxxxx-xxxxx-xxxxx-xxxxx (where 'x' represents a number)
I want to assign this entered string to a variable called installationNumber; I know that it must be a string because it contains dashes between every 5 sets of ints. How is the right way to convert this string to an int using the parse method so I can to mathematical algorithms to it?
Cheers
推荐答案
string input = "99999-99999-99999-99999";
//you can use string replace method, but you need to specify all the replace characters
//to take only the digits of given string
string result1 = new String(input.Where(x=>char.IsDigit(x)).ToArray());//99999999999999999999
//split by delimiter and convert each item to int
int[] values = input.Split('-').Select(i=>int.Parse(i)).ToArray(); //{99999 , 99999 , 99999,99999}
这篇关于使用parse方法将字符串转换为int。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!