开发者

String replacement of `\` not giving what I expect. What gives?

"Action was successful.\\nThank you.".Replace( @"\\", @"\");

yields

"Action was successful.\\nThank you."

What gives?

For the record, I want to store the values in the database with a \n, but at some point du开发者_如何学运维ring getting read back it gets converted into this format. I'm not terribly concerned with stopping that behavior, I want the above listed problem fixed. This way I can gaurantee I'm not getting other garbage in the future.

Also, this doesn't work:

"Action was successful.\\nThank you.".Replace( @"\\n", @"\n" );


In this string "Action was successful.\\nThank you.".Replace( @"\\", @"\"); you have a SINGLE backslash. The \\n is translated in a \ and a n (a backslash + n), so the Replace doesn't replace anything (it's trying to replace TWO backslash with a single backslash.

In the debugger, if you want, you can watch a string in "text mode". If you go to the string value (where it's written what is contained in the string) there should be a down arrow. Click here and click on Text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜