user2060575
user2060575

Reputation: 23

C# - remove whitespace from input number to have int

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

Upvotes: 1

Views: 2919

Answers (4)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

This is another solution.

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

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44605

try this:

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

Upvotes: 1

PhonicUK
PhonicUK

Reputation: 13864

string numberWithoutSpaces = new Regex(@"\s").Replace("12 34 56", "");
int n = Convert.ToInt32(numberWithoutSpaces);

Upvotes: 2

Paul Grimshaw
Paul Grimshaw

Reputation: 21044

Use Replace(...):

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

Upvotes: 5

Related Questions