English 中文(简体)
替换字符串中的反斜线
原标题:Replacing backslash in a string

在C#net的约会线上,

到目前为止,我使用的是:

string.Replace(@"","-")

但它还没有完成替换工作,有人能帮忙吗?

最佳回答

stringing. replace 不修改字符串本身, 但返回新的字符串, 很可能您要丢弃新的字符串。 代之以这样做 :

myString= myString.Replace(@"","-");

顺便提一句,这种操作通常在手动用格式化的日期字符串乱涂的代码中看到。 在大多数情况下,做你想做的事( 也就是? ) 比做这种事情有更好的方法。

问题回答

因为他们都说你需要拿回变量中的值。

因此,它应该是

 val1= val1.Replace(@"","-");

 val1= val1.Replace("\","-");

不仅如此,下面的一个人是行不通的

 val1.Replace(@"","-");

用这种方式

oldstring = oldstring.Replace(@"","-");

查找 String. replace 返回类型。

它的函数返回一个已校正的字符串。 如果它只是更改了旧字符串, 那么它就会有一个 < code> 避免 返回类型 。

您也可以使用 :

myString = myString.Replace( \ ,  - ));

但只是让你知道,日期鞭笞通常是远期鞭笞/ ,而不是反鞭笞

正如其他人建议的那样,字符串. replace 不更新原始字符串对象, 但返回新的字符串 。

myString= myString.Replace(@"","-");

值得你理解的是,字符串在 C# 中是不可更改的, 基本上是为了让它变得安全。 关于字符串的更多细节以及为什么这些字符串是不可更改的, 请参看链接 < a href="http://msdn.microsoft.com/ en- us/library/ 362314fe. aspx" rel= “ nofollow noreferrer" >这里 和 < a href=" https://stackoverflow.com/ questions/2365272/ whis- net-string-is-immomobliable"。 < a>





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...