在C#中将空格转换/替换为%20,反之亦然

4
以下是创建zip文件并向其中添加文件的代码。
问题:当前,当创建zip文件时,其中包含的文件包含%20而不是空格,这是一种功能。我还有另一个要求,即将%20替换为space。如何在下面的代码中实现此目标。
2个回答

8
这里有一个使用Uri静态类的例子:
var url = "http://www.somewhere.net/Thingy%20World.html";

var decoded = Uri.UnescapeDataString(url);
//decoded is currently 'http://www.somewhere.net/Thingy World.html'

var encoded = Uri.EscapeUriString(decoded);//back to encoded
//encoded is currently 'http://www.somewhere.net/Thingy%20World.html' 

0

这应该会有所帮助:

https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring.aspx

取消转义数据字符串是一种简单的方法。您还可以查看正则表达式和Regex.Replace。

编辑:哦 - 您可以使用EscapeDataString将%20放入字符串中。至于正则表达式方法,您可以尝试 Regex encodeSpace = new Regex(" "]); 然后您可以在字符串上调用替换 string newStringWithNoSpaces = encodeSpace.Replace(oldString, "%20"); 您可以为另一个方向执行类似操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接