简体   繁体   中英

Issue with removing symbols from a string

I'd like to remove every "\\n" symbol from every single array element. But after using this code nothing happens;

string[] Words = TextBox.Text.Split(' ');

for (int i = 0; i < Words.Length; i++)
{
    Words[i].Replace("\n", "");
}

What's wrong here?

String.Replace returns a new string; it doesn't affect the original. You need to use

Words[i] = Words[i].Replace("\n", "");

尝试这个

TextBox.Text = TextBox.Text.Replace(Environment.NewLine, string.Empty);

尝试Words[i] = Words[i].Replace("\\n", "");

Strings are immutable. That means that any operation on a string that has already had it's value set, returns a new string and does not change the original string.

As others have said, you need to assign the result of your replace operation to a new string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM