以下 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"
除非我误读了the documentation :
The string that remains after all occurrences of the characters in the trimChars parameter are removed from the start and end of the current String object. If trimChars is null or an empty array, white-space characters are removed instead.
不应该从该输出的第二个字符串中删除尾随双引号吗?
最佳答案
看起来您可能会遇到最后一个双引号后有尾随空格的情况。
尝试:
Console.WriteLine(Environment.CommandLine.Trim().Trim('"'));
看看会发生什么。
您还可以将参数数组中的额外字符传递给您已经在使用的重载:
Console.WriteLine(Environment.CommandLine.Trim('"', ' '));
但是因为我不知道那里有什么样的空格,所以我更喜欢使用删除所有空格的重载而不是猜测那里有哪个字符。
关于c# - 为什么 Environment.CommandLine.Trim ('"') 不删除尾随引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4106041/