为什么Environment.CommandLine.Trim('"') 不删除结尾的引号?

发布于 2024-10-01 07:14:25 字数 748 浏览 0 评论 0 原文

以下 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或空数组,则删除空白字符。

不应该从该输出中的第二个字符串中删除尾部双引号吗?

The following C# code:

using System;
namespace TrimTest {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(Environment.CommandLine);
            Console.WriteLine(Environment.CommandLine.Trim('"'));
            Console.ReadKey(false);
        }
    }
}

produces the following output:

"D:\Projects\TrimTest\TrimTest\bin\Debug\TrimTest.vshost.exe"
D:\Projects\TrimTest\TrimTest\bin\Debug\TrimTest.vshost.exe"

Unless I'm misreading 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.

shouldn't the trailing double-quote be trimmed from the second string in that output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天荒地未老 2024-10-08 07:14:25

看起来您可能会遇到最后一个双引号后面有尾随空格的情况。

尝试:

Console.WriteLine(Environment.CommandLine.Trim().Trim('"'));

看看会发生什么。

您还可以将参数数组中的额外字符传递给您已经使用的重载:

Console.WriteLine(Environment.CommandLine.Trim('"', ' '));

但由于我不知道存在哪种空白,所以我更喜欢使用删除所有空白的重载,而不是猜测那里有哪个字符。

It looks like you could be running in to a situation where there's trailing whitespace after the last double quote.

Try:

Console.WriteLine(Environment.CommandLine.Trim().Trim('"'));

And see what happens.

You could also pass extra characters in the parameter array to the overload you're already using:

Console.WriteLine(Environment.CommandLine.Trim('"', ' '));

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.

伏妖词 2024-10-08 07:14:25

正如贾斯汀所提到的,尾随空格是问题所在。试试这个:

Console.WriteLine(Environment.CommandLine.Trim( new[] {'"', ' '} ));

As alluded to by Justin, trailing whitespace is the issue. Try this:

Console.WriteLine(Environment.CommandLine.Trim( new[] {'"', ' '} ));
月朦胧 2024-10-08 07:14:25

它从头到尾删除,但在你的情况下你看不到也有空白=)
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"

残月升风 2024-10-08 07:14:25

结果最后有一个空格:

        Console.WriteLine(Environment.CommandLine);
        Console.WriteLine(Environment.CommandLine.Trim('"'));

这个有效:

        string commandLine = Environment.CommandLine.Trim(new char[] {'"', ' '});
        Console.WriteLine(commandLine);

Turns out there's a space at the very end:

        Console.WriteLine(Environment.CommandLine);
        Console.WriteLine(Environment.CommandLine.Trim('"'));

This one works:

        string commandLine = Environment.CommandLine.Trim(new char[] {'"', ' '});
        Console.WriteLine(commandLine);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文