【问题标题】:For looping and string对于循环和字符串
【发布时间】:2014-11-13 21:58:20
【问题描述】:

所以我有一个字符串,在其中,我想用一个点替换最后 3 个字符。
我做了一些事情,但我的结果不是我想要的。
这是我的代码:

string word = "To je";
        for (int k = word.Length; k > (word.Length) - 3; k--)
        {
            string newWord = word.Replace(word[k - 1], '.');
            Console.WriteLine(newWord);
        }


我得到的输出是:
对 j。
到.e
To.je

但我想要的输出是:
To...

我如何到达那里?
因此,该程序正在做的事情与我真正想要它做的事情相似,但不完全是。
我真的一直在努力解决这个问题,我们将不胜感激。

【问题讨论】:

  • 你的问题是?
  • Err.. 你的意思是你想用点替换最后三个字符吗?
  • 好的,现在如果原始字符串只有一两个字符怎么办?你还想用点替换它们吗?
  • 试试string newWord = word.Remove(word.Length - numberOfDots) + new string('.', numberOfDots);

标签: c# string loops for-loop


【解决方案1】:

看看这个:

string newWord = word.Replace(word[k - 1], '.');

您总是替换 word... 中的单个字符...但 word 本身并没有改变,因此在下一次迭代中,替换已经“消失”了。

你可以使用:

word = word.Replace(word[k - 1], '.');

(然后将输出移到最后,写出word。)

但是,请注意,这会将所有出现的最后三个字符中的任何一个替换为 .

解决所有这些问题的最简单方法当然是使用Substring,但如果你真的想循环,你可以使用StringBuilder

StringBuilder builder = new StringBuilder(word);
for (int k = word.Length; k > (word.Length) - 3; k--)
{
    builder[k - 1] = '.';
}
word = builder.ToString();

【讨论】:

  • only 理智的方法是子字符串,当前的 .Replace 方法会搞砸像 "aaaaa" 这样的字符串。
  • @mcmonkey4eva:同意。将编辑指出这一点。如果 OP 真的想看到循环运行,还包括使用 StringBuilder 的方法。
【解决方案2】:
  1. 您正在用句点替换最后三个位置中每个字符的所有实例。您只想替换最后一个字符。 “aaaaa”不应该变成“.....”,而应该变成“aa...”。
  2. 您在计算出一个中间值之后打印出newWord,然后再不对其进行任何操作,而使word 保持不变。在正确调整相关字符后,您需要将其分配回 word

当然,更简单的解决方案(对您和计算机而言)是简单地连接您拥有的字符串的一个子字符串,该子字符串不包括带有三个句点的最后三个字符。

【讨论】:

    【解决方案3】:

    假设字符串始终至少包含 3 个字符,您可以将除最后三个字符之外的所有字符都设置为子串,然后将三个点(句点)附加到该字符串的末尾。

    string word    = "To je";
    string newWord = word.Substring(0, word.Length - 3); // Grabs everything but the last three chars
    newWord       += "..."; // Appends three dots at the end of the new string
    
    Console.WriteLine(newWord);
    

    注意:这里假设输入字符串 word 至少是三个字符。如果要提供较短的字符串,则需要对字符串的长度进行额外检查。

    【讨论】:

    • 应该加入一个 if 检查以确保未来情况的长度 > 3(见我的回答)
    【解决方案4】:

    如果以后不需要原词
    使用 Jon Skeets 方法

    string word = "To je";
    word = word.Substring(0,word.Length - 3);
    word += "...";
    
    Console.WriteLine(word);
    

    【讨论】:

    • 这不是一个好的答案。它并不总是用点替换字符串的最后 3 个字符,而是用点替换最后 3 个字符的 all 实例。试试string word = "aaaaa"; - 所有字符将被.替换。
    • Jon Skeet 的回答没有因为同样的缺陷而被否决?
    • 它也应该被否决,但同时他在底部提到了正确的答案,所以......我不知道。
    • 对不起,我只是纠正他的错误,我会更新我的答案
    • 请更正循环条件语法。其中一个逗号应该是分号。
    【解决方案5】:

    正如@jon-skeet 所说,您可以使用子字符串来执行此操作。这里有 3 种方法可以用 substring 做到这一点。

    你可以使用 String.Concat

    string word = "To je";
    word = String.Concat(word.Substring(0,word.Length-3),"...");
    Console.WriteLine(word);
    

    你可以使用 + 运算符

    string word2 = "To je";
    word2 = word2.Substring(0,word.Length-3) + "...";
    Console.WriteLine(word2);
    

    你可以使用 String.Format

    string word3 = "To je";
    word3 = String.Format("{0}...",word3.Substring(0,word.Length-3));
    Console.WriteLine(word3);
    

    【讨论】:

      【解决方案6】:

      我参加聚会有点晚了,但到目前为止发布的所有其他解决方案都不能优雅地处理字符串短于请求的替换数量或任意数量的替换的情况。这是一个通用函数,用于将字符串末尾的最后 n 个字符替换为用户指定的值:

      static String replace_last_n(String s, int nchars, char replacement='.')
      {
          nchars = Math.Min(s.Length, nchars > 0 ? nchars : 0);
          return s.Substring(0, s.Length - nchars) + new String(replacement, nchars);
      }
      
      Console.WriteLine(replace_last_n("wow wow wow", 3));
      Console.WriteLine(replace_last_n("wow", 3, 'x'));
      Console.WriteLine(replace_last_n("", 3));
      Console.WriteLine(replace_last_n("w", 3));
      Console.WriteLine(replace_last_n("wow", 0));
      Console.WriteLine(replace_last_n("wow", -2));
      Console.WriteLine(replace_last_n("wow", 33, '-'));
      

      输出:

      wow wow ...
      xxx
      
      .
      wow
      wow
      ---
      

      【讨论】:

        【解决方案7】:
        if (word.Length > 3)
            Console.WriteLine(word.substring(0, word.Length - 3) + "...");
        

        或类似的东西,不需要循环!

        【讨论】:

          猜你喜欢
          • 2013-12-30
          • 2012-07-10
          • 1970-01-01
          • 2012-03-09
          • 1970-01-01
          • 2017-12-24
          • 2015-04-21
          • 2016-02-08
          • 2019-10-03
          相关资源
          最近更新 更多