我必须在C#中用\替换\\ [英] I have to replace \\ with \ in C#

查看:169
本文介绍了我必须在C#中用\替换\\的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在文本框中输入

 T4INTERN  -  03 \SQLEXPRESS 

,然后将其附加到字符串构建器

 StringBuilder ConStr = new StringBuilder。

 ConStr.Append(txtServerName.Text.Trim()); 

然后我将该字符串存储在

字符串connection_string = ConStr.ToString( ); 



但它给了我这样的

 T4INTERN  -  03 \\SQLEXPRESS。

我必须删除它从它开始。



我尝试过:



我试过

 connstring = connstring.Replace(@\\,@\); 

但它无效。

解决方案

这可能听起来很奇怪,但并非所有内容都是......

调试器将字符串值显示为转义字符串,因此(双引号)将显示为\和\(斜杠)将显示为\\ ...

有哪些字符转义序列可用? | C#常见问题解答 [ ^ ]



通过在变量名称中添加',nq',可以告诉Visual Studio不要这样做在观察窗口......



格式C#中的说明符 [ ^ ]

I has enter

T4INTERN - 03\SQLEXPRESS

in textbox, then it is append in the string builder

StringBuilder ConStr = new StringBuilder.

ConStr.Append(txtServerName.Text.Trim());

Then i store that string in

string connection_string =ConStr.ToString();


But it gives me like this

T4INTERN - 03\\SQLEXPRESS.

I have to remove that one \ from it.

What I have tried:

I have tried

connstring=connstring.Replace(@"\\",@"\");

But it is not working.

解决方案

It may sound strange but not all is what it seems to be...
The debugger will display string values as escaped string so " (double quote) will show as \" and \ (slash) will show as \\...
What character escape sequences are available? | C# Frequently Asked Questions[^]

You can tell Visual Studio not to do, by adding ',nq' to the variable name in the watch window...

Format Specifiers in C#[^]


这篇关于我必须在C#中用\替换\\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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