user5193098
user5193098

Reputation:

How can I remove all whitespace from string?

I've tried following code:

foreach (string item in strArr)
{
    item.Replace(" ", "");
    System.IO.Directory.CreateDirectory(foldercreationPATH.Text + "\\final\\" + item);
};

but it doesn't do anything. If I add return before item.Replace, then it throws errors:

Method must have a return type and return must not be followed by an object expression!

Thanks for help in advance!

Upvotes: 1

Views: 89

Answers (2)

Hedego
Hedego

Reputation: 294

You can try the following.

var item = item.trim();

Upvotes: 0

P Lysenius
P Lysenius

Reputation: 1133

You need to assign your variable with the new value.

item = item.Replace(" ", "");

Or just put the Replace inside the method.

System.IO.Directory.CreateDirectory(foldercreationPATH.Text + "\\final\\" + item.Replace(" ", ""));

Upvotes: 3

Related Questions