C#获取字符串的总数 [英] C# getting the total of a string

查看:351
本文介绍了C#获取字符串的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在使用某个应用时遇到了问题,它基本上是相加的数字(a + b = x),但是第一个输入的单个数字需要相加等于5,例如14 = 1 + 4 = 5、32 = 3 +2 = 5,依此类推.

Hi, im having problems with a app, its basically adding up to number (a + b = x) but the first inputs individual digits need to add up to 5 for example 14 = 1+4 = 5, 32 = 3+2 = 5 and so on. anyone have any advice on how to do this?

推荐答案



这个快速又肮脏的代码应该可以满足您的需求:
Hi,

this quick and dirty code should do what you need:
string numbers = "14";
int first = 0;
Int32.TryParse(numbers.Substring(0, 1), out first);
int second = 0;
Int32.TryParse(numbers.Substring(1, 1), out second);
int result = first + second;
MessageBox.Show(result.ToString());


您可以通过使用简单编码来实现

you can do it by using Simple coding

string str = "14"; // you can enter any two digit no like 25,30 etc
           int res = int.Parse(str.Substring(0, 1)) + int.Parse(str.Substring(1, 1));
           Console.WriteLine(res); // output 5


这将起作用根据您的要求输入任意数量.
This will work according to your requirement for any number.
string numbers = "221";
 //string numbers = "11111";
   int tot = 0;
   for (int i = 0; i <= numbers.Length - 1; i++)
   {
       int cnt = int.Parse(numbers[i].ToString());
       tot += cnt;
   }MessageBox.Show(tot.ToString());


这篇关于C#获取字符串的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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