C# - remove whitespace from input number to have i

2019-07-31 03:11发布

问题:

How to remove white space from inside of input number:

1 987 to 1987 - I need input number to be int for the rest of script:

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

回答1:

Use 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);


回答3:

try this:

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


回答4:

This is another solution.

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


标签: c# whitespace