8

how to replace any charachter with nothing. for example

1 : string inputString = "This Is Stack OverFlow";
2 : string outputString = inputString.Replace(' ', '');

expected output = "ThisIsStackOverFlow";

I tried line 2 but it says empty Character String in Replace methods second argument. Can any body tell me whats wrong with my Replace Method and why it does not take blank like we normally use in string (for ex : "")?

Andrew
  • 7,602
  • 2
  • 34
  • 42
Ashwin
  • 431
  • 1
  • 5
  • 11
  • possible duplicate of [Remove characters from C# string](http://stackoverflow.com/questions/7411438/remove-characters-from-c-sharp-string) – Connor McGuinness Nov 10 '14 at 13:19
  • Sorry for posting such a easy Question, i should have looked for its overloaded methods and i got the answer, its Replace (string,string) method which can do my work, but still want to know why empty didn't work in second argument of Repalce(char, char) method – Ashwin Nov 10 '14 at 13:20
  • As I stated in my answer, `''` isn't a valid character. You actually want *no character*, so you can't use that notation and you need to use the `string` one. The closest option is a null character, but you don't want that. – Andrew Nov 10 '14 at 13:23

1 Answers1

20

Because you are using the Replace(char, char) overload instead of Replace(string, string) ('' is not a valid character). Just use this:

string ouputString = inputString.Replace(" ", "");
Andrew
  • 7,602
  • 2
  • 34
  • 42