使用parse方法将字符串转换为int。 [英] convert a string to an int using parse method.

查看:100
本文介绍了使用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.Replace 将所有连字符替换为 string.empty 然后转换为 Int64
Use String.Replace to replace all the hyphens with string.empty and then convert to Int64.


您可能无法将此大数字转换为整数,您将获得溢出异常。



我不确定您的要求是什么,但如果您需要进行数学计算,请尝试下面



you may not convert this large number to integer, you will get overflow exception.

i'm not sure what is your requirement but if you need do mathematical calculations try below

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。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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