为什么Environment.CommandLine.Trim('"') 不删除结尾的引号?
以下 C# 代码:
using System;
namespace TrimTest {
class Program {
static void Main(string[] args) {
Console.WriteLine(Environment.CommandLine);
Console.WriteLine(Environment.CommandLine.Trim('"'));
Console.ReadKey(false);
}
}
}
产生以下输出:
"D:\Projects\TrimTest\TrimTest\bin\Debug\TrimTest.vshost.exe"
D:\Projects\TrimTest\TrimTest\bin\Debug\TrimTest.vshost.exe"
除非我误读 文档:
从当前 String 对象的开头和结尾删除所有出现的 trimChars 参数中的字符后保留的字符串。如果trimChars为null或空数组,则删除空白字符。
不应该从该输出中的第二个字符串中删除尾部双引号吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您可能会遇到最后一个双引号后面有尾随空格的情况。
尝试:
看看会发生什么。
您还可以将参数数组中的额外字符传递给您已经使用的重载:
但由于我不知道存在哪种空白,所以我更喜欢使用删除所有空白的重载,而不是猜测那里有哪个字符。
It looks like you could be running in to a situation where there's trailing whitespace after the last double quote.
Try:
And see what happens.
You could also pass extra characters in the parameter array to the overload you're already using:
But since I don't know what kind of whitespace there is, I prefer to use the overload that removes ALL whitespace rather than guess which character is there.
正如贾斯汀所提到的,尾随空格是问题所在。试试这个:
As alluded to by Justin, trailing whitespace is the issue. Try this:
它从头到尾删除,但在你的情况下你看不到也有空白=)
ion15.vshost.exe”
It removes from the very start and end, but in your case u cant see that there is a white space too =)
ion15.vshost.exe"
结果最后有一个空格:
这个有效:
Turns out there's a space at the very end:
This one works: