字符串替换转义字符

编程入门 行业动态 更新时间:2024-10-25 22:27:55
本文介绍了字符串替换转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

今天,我发现把字符串资源文件将导致他们被视为文字,即把为第一行文字\\\为第二行文本将导致转义字符本身成为逃脱,等什么是存储的第一行文字\\\为第二行文字 - 然后将这些在显示出来的,而不是我的回车和标签

那么什么我想要做的是使用与string.replace把 \\ 到 \ - 这并未 :T似乎工作

s.Replace(\\\\,\\);

,因为字符串认为有只1反斜杠

s.Replace(\\,​​);

替换所有的双引号,并给我留下了仅仅局限于N,而不是\ ñ

此外,使用 @ 和一半的 \ 字符或 Regex.Replace 方法产生相同的结果。

任何人都知道的一个好办法做到这一点未经字符通过字符的循环?

解决方案

由于 \\\ 是其实一个字符,你不能简单地将字符串替换反斜线达致这。您将需要转义字符替换每一对 \ 和下面的字符,如:

s.Replace(\\\\,\\\); s.Replace(\\t,\t); 等

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

So what I'd like to do is use string.replace to turn \\ into \ - this doesn't seem to work.

s.Replace("\\\\", "\\");

doesn't change the string at all because the string thinks there's only 1 backslash

s.Replace("\\", "");

replaces all the double quotes and leaves me with just n instead of \n

also, using @ and half as many \ chars or the Regex.Replace method give the same result

anyone know of a good way to do this without looping through character by character?

解决方案

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:

s.Replace("\\n", "\n"); s.Replace("\\t", "\t"); etc

更多推荐

字符串替换转义字符

本文发布于:2023-10-13 17:16:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!