【问题标题】:c# TrimEnd removes more than needed?c# TrimEnd 删除了超出需要的内容?
【发布时间】:2013-11-27 07:38:53
【问题描述】:

这里是调试器。 我不明白为什么 TrimEnd 删除 's' 字符。 在 TrimEnd() 之后 有什么建议吗?

recPath 是类中的私有字符串。 代码:

        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(@"\\", @"\");

【问题讨论】:

  • 将您的代码显示为文本,而不是图像..

标签: c# .net windows registry


【解决方案1】:

问题是您传入lastkey.ToCharArray() 作为要修剪的字符列表。这包括字符s,因此Fontss 也被修剪。 (反斜杠同上。)来自TrimEnd 的文档:

TrimEnd 方法从当前字符串中删除 trimChars 参数中的所有尾随字符。当在字符串末尾遇到不在 trimChars 中的第一个字符时,修剪操作停止。

我怀疑你希望这些字符被用作单个字符串。

如果您只想从recPath 的末尾删除lastKey,您可以使用:

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

【讨论】:

    【解决方案2】:

    MSDN:

    TrimEnd 方法从当前字符串中删除所有尾随 trimChars 参数中的字符。 当在字符串末尾遇到不在 trimChars 中的第一个字符时,修剪操作停止。

    基本上,在您的 lastKey 字符串中,您有 char '\' 和 char 's',但没有 't'...所以它会删除所有最后的字符并停在 "字体”。

    TrimEnds 不会删除连续顺序的字符。请改用子字符串(或 LINQ)。

    【讨论】:

      【解决方案3】:

      您提供了一个字符串logPixels--50\\\\,转换为char数组后它包含s和其他字符。

      现在trimEnd 开始在字符串末尾查找这些字符并继续删除,直到它到达不在数组中的font 中的t,它停止工作。

      【讨论】:

        猜你喜欢
        • 2013-05-25
        • 2016-03-15
        • 1970-01-01
        • 1970-01-01
        • 2020-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多