Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

c# - 已解决数学最后一个str + 1 return 50(Solved Mathing Last str + 1 return 50)

Hello iam trying to math this to get result = 2

(您好,IAM尝试对此进行数学运算以获得结果= 2)

string str = "1005480701";
Console.WriteLine(str.Last() + 1);

it should be 2 but the result is 50 can someone explain why that happen please

(应该是2,但结果是50,有人可以解释为什么会发生吗?)

  ask by AlexN translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

string.Last() returns the char '1' .

(string.Last()返回char '1')

The UTF-16 code for '1' is 49 and if you add 1 to 49 , you get 50 .

('1'的UTF-16代码为49 ,如果将1加到49 ,则得到50 。)

You have to transform the character to the number it represents, for example with int.Parse() .

(您必须将字符转换为它表示的数字,例如使用int.Parse() 。)

...
Console.WriteLine(int.Parse(str.Last().ToString()) + 1);
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...