c# - 为什么 Environment.CommandLine.Trim ('"') 不删除尾随引号?

标签 c# .net clr 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"

除非我误读了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/

相关文章:

c# - 在 ASP.NET MVC 中调用任何 Controller 操作之前处理 cookie

c# - 连接字符串时的新引用

c++ - 如何指定 GetDelegateForFunctionPointer 的调用类型 System : Func?

c# - 我应该在终端服务上调用 Application.EnableVisualStyles() 吗?

c# - 计算异步按钮被按下的次数(或者是否正在进行某些操作)

c# - .net 中平等检查的 5 种方法 .. 为什么?以及使用哪个?

c++ - 将值 C#/COM 返回到 C++ 客户端?

c++ - 启用 Common Lanuage Runtime 时 OleInitialize 失败?

c# - 抑制条件方法的代码分析警告

c# - 如何使用 C# 远程对 Stack Overflow 帖子投反对票?