views:

225

answers:

4

Ok, I haven't programmed in C# before but I came upon this code and was wondering what it does. Now, I now that it just searches and find the first occurrence of "." and replaces it with "" but what exactly is in the ""? Would this just delete the period or is there a space character that replaces the "."? I'm trying to figure out how to transfer this method into Objective-C, but I need to know if the period is replaced by no characters or a space character.

someString.Replace(".", "")
+11  A: 

"" is just an empty string. Your code example replaces all occurrences of the periods with no characters.

(Note that the original string is untouched, and the return value of that line of code will be the modified string.)

It is actually better to use string.Empty rather than "". This is because string.Empty is much more readable, and is just an alias for "", so there is no performance consideration. Not to mention, if you use StyleCop, it will tell you not to use "".

womp
And so it's easier to tell if there's a space in there or not ;)
colithium
That's for sure!
CalebHC
The performance has to do with string interning and there only being a single instance of the empty string (which String.Empty points to)
Ed Swangren
@Ed Swangren - "" will be interned as well - even if it's not automagically replaced by string.Empty by the compiler. http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx
Mark Brackett
Right, that's what I was trying to say...
Ed Swangren
+1  A: 

It replaces by no characters, an empty string.

John Saunders
+7  A: 

No characters. This code removes periods from a string... sort of. The way it should REALLY be called is:

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

(or as the other guys say, it REALLY should be)

someString = someString.Replace(".", String.Empty);
Dave Markle
A: 

Is replaced by no characters at all. If you want to locate a white character you need to use " "

backslash17