0

I'm making a program. Now to write the data in the file I need to replace the spaces in the string obtained with lets say a # symbol.

Is there any command in C# that lets me do this without looping through the whole string?

4 Answers 4

8

Sure, use the Replace() method.

s = s.Replace(" ", "#");

(And if you want people here to want to help you in the future, my recommendation would be to start accepting some answers. Just a thought.)

Sign up to request clarification or add additional context in comments.

Comments

0

To replace # with text

string s = "test # again";
s = s.Replace("#", "Superman");

To replace space with another character

string s = "test again";
s = s.Replace(' ', '#');

Comments

0

Be sure to retrieve the returned string from the Replace() method as it won't change the original string...it generates a new one.

Comments

0

Did you try using the Replace method of the string object. This will do the trick:

string newString = oldString.Replace(" ", "#");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.