c# TrimEnd removes more than needed?

Here is debugger. I don't understand why TrimEnd delete 's' character. Before TrimEnd After TrimEnd() After TrimEnd Any suggestions?

recPath is private string in class. code:

        recPath = "";
        recursiveFindPathRoot(node);
        string[] argv = Regex.Split(recPath, "\\\\");

        //Current root path
        string rootdat = argv[0];

        //Current lastkey
        string valdat = argv[argv.Length - 3];
        string lastkey = valdat + "\\\\";

        string[] val_dat =  Regex.Split( valdat , "--");

        //Getting value and data
        string value = val_dat[0];
        string data = val_dat[1];
        string caption = value;

        CollectDataInput("Please edit selected key", caption, out value, out data);

        recPath = recPath.TrimEnd(lastkey.ToCharArray());
        recPath = recPath.Replace(@"\\", @"\");
Jon Skeet
people
quotationmark

The problem is that you're passing in lastkey.ToCharArray() as the list of characters to trim. That includes the character s, so the s of Fonts is being trimmed as well. (Ditto the backslash.) From the docs for TrimEnd:

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string.

You're expecting the characters to be used as a single string, I suspect.

If you just want to remove lastKey from the end of recPath, you can use:

if (recPath.EndsWith(lastKey))
{
    recPath = recPath.Substring(0, recPath.Length - lastKey.Length);
}

people

See more on this question at Stackoverflow