Josh Bradley
Josh Bradley

Reputation: 4800

C# Strings - Simple syntax Question

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(".", "")

Upvotes: 1

Views: 317

Answers (4)

womp
womp

Reputation: 116977

"" 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 "".

Upvotes: 11

Dave Markle
Dave Markle

Reputation: 97671

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

Upvotes: 7

backslash17
backslash17

Reputation: 5390

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

Upvotes: 0

John Saunders
John Saunders

Reputation: 161773

It replaces by no characters, an empty string.

Upvotes: 1

Related Questions