开发者

concatenate characters and convert to int

i am using c# and i need some help in this... i have 2 strings

s1="1234"
s开发者_运维百科2="5678"

i want to create 2 integers so that they become equal to the 1st 2 characters and convert to int. ie

int i1=12  12 is the 1st 2 characters from s1
int i2=56  56 is the 1st 2 characters from s2


Simple:

int i1 = Convert.ToInt32(s1.Substring(0, 2));
int i2 = Convert.ToInt32(s2.Substring(0, 2));

You'll have to add various checks, though (say, what happens if the length of either string is less than 2? or if they contain something other than digits?)


int i1 = Convert.ToInt32(s1.substring(0,2));

And the same for i2 of course, obviously not great using hard-coded index of 2, but you could specify that in some other way through user input or whatever?


int i1 = int.parse(s1.SubString(0,2));
int i2 = int.parse(s2.SubString(0,2));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜