C#如何更换斜线报价\" [英] C# how to replace Slash Quote \"

查看:145
本文介绍了C#如何更换斜线报价\"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个搜索和替换字符串是这样的:

I'm trying to create a search and replace string like this:

SRC = \/path/file.jpg\\ \\

SRC = \ http://mysite.com/path/file.jpg\

通过搜索属性src和等号削减报价。问题是创建搜索字符串,每次我这样做的时候,它成为

By searching for the property src and the equals slash quote. The problem is creating the search string, every time I do it, it becomes a search for

SRC = \\/ 而不是 SRC = \/

如果属性=在这一领域SRC ,我怎样才能得到它的工作



字符串equalsSlashQuote ==+ @\+\?;

字符串搜索=财产+ equalsSlashQuote +/;

字符串替换= +物业+ equalsSlashQuote SITEURL +/;

输入= input.Replace(搜索,替换);



的问题是\,我甚至尝试添加它作为字符码值92,它仍然成为\\在搜索变量。

if property = "src" in this segment, how can I get it to work?

string equalsSlashQuote = "=" + @"\" + "\"";
string search = property + equalsSlashQuote + "/";
string replace = property + equalsSlashQuote + SiteURL + "/";
input = input.Replace(search, replace);

The problem is the \, I even tried adding it as the char code value 92 and it still becomes \\ in the search variable.

推荐答案

如果您在字符串前加上一个@,字符串becames文字,而无需使用控制字符或转义(用在backslahses)。

If you put an @ before the string, the string becames "literal" without using control characters or escapes (with the backslahses).

所以 @hello\\\
ico
将导致上写着一个字符串Hello 和尼科被换行分隔你好和ICO,一个斜杠,而不是单词分开。

So @"hello\nico" will result in a string with the words "hello" and "nico" separated by a slash and not the words "hello" and "ico" separated by a line feed.

您也可以定义字符串没有@并与控制字符这样的:hello\\\\
ico
这将有同样的结果。 。第一个反斜杠是控制字符,而不是字符串中的实际值

You can also define a string without the @ and with control characters like this: "hello\\nico" which will have the same result. The first backslash is a control character, not an actual value inside the string.

注意:如果你的IDE /调试器显示的字符串值,在还将使用第二格式来显示文本。因此字符串里的反斜杠将通过把另一个反斜杠之前被转义。它看起来像字符串包含双斜线,但事实并非如此。您可以验证这一点:

Be aware: if your IDE / debugger shows a value of a string, at will also use the second format to display the text. So the backslash inside the string will be escaped by putting another backslash before it. It looks like the string contains double slashes, but it does not. You can verify this by:


  • 检查字符串的长度和计数的字符

  • 写这样的值来输出的Debug.WriteLine Console.WriteLine 它将显示出字符串,因为它是无斜线为逃逸

  • Checking the length of the string and count the characters.
  • Write the value to an output like Debug.WriteLine or Console.WriteLine which will show that string as it is, without the slashes as escapes.

您说的:的每次我这样做,就变成了一个搜索 SRC = \\/ 而不是 SRC = \/ 的你确定吗?我觉得你被IDE /调试器,将显示一个与第二个反斜杠,这仅仅是一个控制字符字符串上当。

You say: Every time I do it, it becomes a search for src=\\"/ instead of src=\"/. Are you sure? I think you are fooled by the IDE / debugger which will show the string with an second backslash, which is just a control character.

这篇关于C#如何更换斜线报价\"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆