user6274399
user6274399

Reputation: 65

Replace does not work in some scenarios

I have the following string.

We all know you can never have too many yoga pants!! Today's giveaway is with my fav @aloyoga \ud83d\ude0d \ud83c\udf81\ud83d\udcaa\ud83c\udffcI am honestly so proud of everyone who has been participating in the #JenSelterChallenge.. I have been overwhelmed by the level of commitment, progress, and positivity over the last couple of weeks. Seeing your photos and captions puts a huge smile on my face everyday. Thank you guys for making it such a success and for those who haven't joined yet, it's not too late! Congrats to the winners of WEEK 3\u2014 I will DM you all this weekend!! \ud83d\ude03 \n@Vicfitness_\n@Bodyroxxfit\n@Krvstah\n@Lvlelissa3\n@ThisIsNaila \n@X_i_a_r_a_\n@Btrevellini\n@Nicolesfitjourney_ \n#Seltering

I want to replace the \n with Environment.NewLine. So, I tried the following:

caption = caption.Replace("\n", Environment.NewLine);

Did not work. Nothing was changed. I tried at the previous step when I declare it also,

caption = Regex.Match(response, pattern).Groups[1].Value.Replace("\n", Environment.NewLine);

Also didn't work.

However, when I manually try to input the string like this:

string test = "We all know you can never have too many yoga pants!! Today's giveaway is with my fav @aloyoga \ud83d\ude0d \ud83c\udf81\ud83d\udcaa\ud83c\udffcI am honestly so proud of everyone who has been participating in the #JenSelterChallenge.. I have been overwhelmed by the level of commitment, progress, and positivity over the last couple of weeks. Seeing your photos and captions puts a huge smile on my face everyday. Thank you guys for making it such a success and for those who haven't joined yet, it's not too late! Congrats to the winners of WEEK 3\u2014 I will DM you all this weekend!! \ud83d\ude03 \n@Vicfitness_\n@Bodyroxxfit\n@Krvstah\n@Lvlelissa3\n@ThisIsNaila \n@X_i_a_r_a_\n@Btrevellini\n@Nicolesfitjourney_ \n#Seltering";
test.Replace("\n", Environment.NewLine);

It works fine. But I'm not sure what is different when I hard-code the string and when I get it normally. It's the exactly same string, I have checked it myself by debugging. Is there something I'm not aware of?

Upvotes: 2

Views: 64

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

When you do "\n" that gets turned in to the single character 0x0A. What it looks like you actually want are the two characters "\" and "n" next to each other.

Put a @ in front of your replace string and it will stop translating the \n to 0x0A

caption = caption.Replace(@"\n", Environment.NewLine);

You could also do it by escaping the slash

caption = caption.Replace("\\n", Environment.NewLine);

Upvotes: 7

mariosangiorgio
mariosangiorgio

Reputation: 5543

Does it work if you do caption.Replace("\\n", Environment.NewLine);?

It looks like your string is not containing the newline character '\n' but a string composed by a backslash followed by the character n.

Upvotes: 0

Related Questions