2016-09-21 96 views
-5

我有两个字符串string Astring B添加的字符串C#的数字

string A = "24"string B = "30"我想这些数字添加到string C

C=A+B(I know this is wrong format)我想知道如何添加值(数字)串

+2

为什么他们甚至是字符串开头?将它们定义为整数并使用'+'运算符。 – ThePerplexedOne

+0

如何用int值定义字符串? –

+1

如果字符串不包含数字,你想怎么做? – Bathsheba

回答

0

做如下的功能:

public static string sum(string A,string B) 
{ 
      int i = Convert.ToInt16(string.IsNullOrEmpty(A) ? "0" : A); 
      int j = Convert.ToInt16(string.IsNullOrEmpty(B) ? "0" : B); 

      int k = i+j; 

      return k.ToString(); 
} 

并如下使用它:

string C = sum("1","2");