How can I replace a character in a string with som

2019-08-20 02:54发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-08-20 03:21

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

查看更多
放我归山
3楼-- · 2019-08-20 03:24

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

string newString = oldString.Replace(" ", "#");
查看更多
男人必须洒脱
4楼-- · 2019-08-20 03:37

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(' ', '#');
查看更多
Rolldiameter
5楼-- · 2019-08-20 03:41

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

查看更多
登录 后发表回答