2013-02-11 31 views
1

如何從輸入數字裏面刪除空白:C# - 從輸入號碼刪除空格有INT

1 987年到1987年 - 我需要輸入編號爲int的腳本的其餘部分:

int n = Convert.ToInt32(args.Content); 
      if (n >= 1000) 
       n = (int) (n - (n * 0.75)); 
+1

替換爲空字符串空間? – 2013-02-11 10:06:21

回答

5

使用replace(...):

int n = Convert.ToInt32(args.Content.Replace(" ","")); 
if (n >= 1000) 
n = (int) (n - (n * 0.75)); 
2
string numberWithoutSpaces = new Regex(@"\s").Replace("12 34 56", ""); 
int n = Convert.ToInt32(numberWithoutSpaces); 
1

試試這個:

int n = Convert.ToInt32(args.Content.Replace(" ", string.Empty); 
+0

錯誤更正:-)和+1 – 2013-02-11 10:08:30

0

這是另一種解決方案。

string str = new string(args.Where(c => c != ' ').ToArray()); 
int n = Convert.ToInt32(str);