且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串转换为 int 然后再转换回字符串

更新时间:2022-03-30 05:34:30

关键在于操作的优先级:

It's all about the priority of the actions:

int.Parse (str_Val + 1)

在上面的行中,加法发生在 str_Val + 1 输出 11,111,111 等.

In the row above first the addition happens str_Val + 1 outputing 11,111,111 etc.

然后解析发生将 "11" 更改为 11

Then the parsing occurs changing "11" to 11

然后字符串发生将 11 更改为 "11"

Then to string occurs changing 11 to "11"

因此将您的代码更改为

str_Val  = (int.Parse(str_Val)+1).ToString();

这将首先将字符串转换为 int,然后添加两个整数,最后再次将整数转换为字符串.

This will first convert the string to int, then add two integers and finally convert the integer to string again.