2016-09-21 138 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");