取代净斜杠 [英] replace slashes in .Net

查看:141
本文介绍了取代净斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地文件路径包含\,我需要改变所有出现的/为远程文件的路径。

I have a local file path containing "\" and I need to change all occurrences to "/" for a remote file path.

我已经试过

myString.replace("\","/")

myString.replace(Convert.ToChar(92), Convert.ToChar(47)) 

似乎都留在机智的\..

Both seem to leave the "\" in tact..

答:

NewString = myString.replace("\","/")

但问题是,我没有它赋值给一个变量。逃脱斜线实际上是由它失败了,在vb.net中最少。

The problem was that I was not assigning it to a variable. Escaping the slash actually made it fail, in vb.net at least.

推荐答案

字符串是不可变的。该替换方法返回一个新字符串,而不是影响当前字符串,因此,你需要捕获结果的变量。如果你使用VB.NET没有必要逃避反斜杠,但在C#中,必须通过他们的使用2转义。

Strings are immutable. The Replace method returns a new string rather than affecting the current string, therefore you need to capture the result in a variable. If you're using VB.NET there's no need to escape the backslash, however in C# it must be escaped by using 2 of them.

VB.NET(没有逃脱需要):

VB.NET (no escaping needed):

myString = myString.Replace("\","/")

C#(反斜杠转义):

C# (backslash escaped):

myString = myString.Replace("\\","/");

我假设你正在使用VB.NET,因为你不包括分号,也没有逃脱反斜杠,并且由于使用的替代方法的外壳。

I assume you're using VB.NET since you don't include a semicolon, didn't escape the backslash and due to the casing of the replace method used.

这篇关于取代净斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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