Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Environment.CommandLine.Trim('"') not remove the trailing quote?

Tags:

c#

.net

trim

clr

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?

like image 327
Dylan Beattie Avatar asked Oct 14 '22 21:10

Dylan Beattie


1 Answers

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.

like image 82
Justin Niessner Avatar answered Oct 18 '22 03:10

Justin Niessner