-3

I have variables like this:

var a = "a variable";
var b = "test";

Is there a simple function I could call on the variables that would remove the spaces?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

2 Answers2

2

It sounds like you're looking for someString.Replace(" ", "").

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Do you mean something like this:

var aWithoutSpaces = a.Replace(" ", string.Empty);
Silas
  • 1,140
  • 11
  • 12
  • Why the heck (!) would I use `string.Empty` here instead of `""`? It’s an eyesore. – Konrad Rudolph Nov 06 '11 at 14:39
  • 1
    @KonradRudolph: No it's not. It's a people choice, that's all. You downvoted for that? – Otiel Nov 06 '11 at 14:41
  • 1
    @Otiel “people’s choice” is not a good excuse for poor choices. And no, I didn’t downvote for that. Somebody else must have. Though I do understand the rationale. – Konrad Rudolph Nov 06 '11 at 14:41
  • 1
    @KonradRudolph: I wouldn't call that as a _"poor choice"_. There's no evil in using `string.Empty` instead of `""`. – Otiel Nov 06 '11 at 14:44
  • 2
    Readability. And AFAIK ([and](http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or) [read](http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx)) there's nothing wrong with using string.Empty. I would be happy if somebody could explain the problem this string.Empty (and the downvote). – Silas Nov 06 '11 at 14:52
  • What conceivable argument could there be that `string.Empty` would be more readable than `""`? This is just completely far-fetched. I know the other arguments – the blog post is ancient and most probably outdated, especially the discussion. – Konrad Rudolph Nov 06 '11 at 15:06
  • 1
    string.Empty is just more meaningful than a more or less cryptic "". Especially when standing next to " ". Let me ask the other way round: Why do you think it's _not_ readable but an eyesore. Of course there are different opinions about good programming styles and my one is influenced a lot by Unlce Bob's "Clean Code" regarding self-explanatory identifiers. But regardless of that string.Empty is a valid answer to the given question. – Silas Nov 06 '11 at 15:14