C# 字符串替换转义字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/357804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
string replace on escape characters
提问by LoveMeSomeCode
Today I found out that putting strings in a resource file will cause them to be treated as literals, i.e putting "Text for first line \n Text for second line" will cause the escape character itself to become escaped, and so what's stored is "Text for first line \n Text for second line" - and then these come out in the display, instead of my carriage returns and tabs
今天我发现将字符串放在资源文件中会导致它们被视为文字,即放置“第一行文本\n第二行文本”将导致转义字符本身被转义,因此存储的是“第一行的文本 \n 第二行的文本” - 然后这些出现在显示中,而不是我的回车和制表符
So what I'd like to do is use string.replace to turn \\
into \
- this doesn't seem to work.
所以我想做的是使用 string.replace\\
变成\
- 这似乎不起作用。
s.Replace("\\", "\");
doesn't change the string at all because the string thinks there's only 1 backslash
根本不改变字符串,因为字符串认为只有 1 个反斜杠
s.Replace("\", "");
replaces all the double quotes and leaves me with just n instead of \n
替换所有双引号,只留下 n 而不是 \n
also, using @
and half as many \
chars or the Regex.Replace
method give the same result
此外,使用@
和一半的\
字符或Regex.Replace
方法给出相同的结果
anyone know of a good way to do this without looping through character by character?
任何人都知道不逐个字符地循环执行此操作的好方法吗?
采纳答案by codelogic
Since \n
is actually a single character, you cannot acheive this by simply replacing the backslashes in the string. You will need to replace each pair of \
and the following character with the escaped character, like:
由于\n
实际上是单个字符,因此您无法通过简单地替换字符串中的反斜杠来实现这一点。您需要\
用转义字符替换每对和以下字符,例如:
s.Replace("\n", "\n");
s.Replace("\t", "\t");
etc
回答by John Rudy
You'd be better served adjusting the resx files themselves. Line breaks can be entered via two mechanisms: You can edit the resx file as XML (right-click in Solution Explorer, choose "Open As," and choose XML), or you can do it in the designer.
您最好自行调整 resx 文件。可以通过两种机制输入换行符:您可以将 resx 文件编辑为 XML(在解决方案资源管理器中右键单击,选择“打开为”,然后选择 XML),或者您可以在设计器中执行此操作。
If you do it in the XML, simply hit Enter, backspace to the beginning of the newline you've created, and you're done. You could do this with Search and Replace, as well, though it will be tricky.
如果您在 XML 中执行此操作,只需按 Enter,退格到您创建的换行符的开头,就完成了。您也可以使用“搜索和替换”来执行此操作,尽管这会很棘手。
If you use the GUI resx editor, holding down SHIFT while pressing ENTER will give you a line break.
如果您使用 GUI resx 编辑器,按住 SHIFT 的同时按 ENTER 会给您换行。
You could do the run-time replacement thing, but as you are discovering, it's tricky to get going -- and in my mind constitutes a code smell. (You can also make a performance argument, but that would depend on how often string resources are called and the scale of your app in general.)
你可以做运行时替换的事情,但正如你发现的那样,开始是很棘手的——在我看来,这构成了代码异味。(您也可以提出性能参数,但这取决于调用字符串资源的频率以及您的应用程序的总体规模。)
回答by LoveMeSomeCode
I'm actually going with John's solution and editing the XML directly as that's the better solution for the project, but codelogic answered the question that was driving me insane.
我实际上使用 John 的解决方案并直接编辑 XML,因为这是项目的更好解决方案,但 codelogic 回答了让我发疯的问题。