如何将给定字符串中的所有字符实例加倍。使用c#

如何将给定字符串中的所有字符实例加倍。使用c#,c#,string,C#,String,例如,字符串“ABCXYZ”中“X”的所有实例都加倍将变为“ABCXYZ” 我想用这种方法来解决: 将字符串复制到char数组 遍历整个数组,找出字符串中“X”的出现次数 用#发生次数*2找出新的长度 对字符数组进行迭代,每次出现“X”时存储“X”两次 将字符数组复制回字符串 在c#中有没有一种有效的方法可以做到这一点,请推荐一种 谢谢该方法可以做到这一点: "ABCXYZ".Replace("X","XX") 如果您事先不知道是哪个角色,可以使用: 我认为,Replace将为您提供正确的字符

例如,字符串“ABCXYZ”中“X”的所有实例都加倍将变为“ABCXYZ”

我想用这种方法来解决:

  • 将字符串复制到char数组
  • 遍历整个数组,找出字符串中“X”的出现次数
  • 用#发生次数*2找出新的长度
  • 对字符数组进行迭代,每次出现“X”时存储“X”两次
  • 将字符数组复制回字符串
  • 在c#中有没有一种有效的方法可以做到这一点,请推荐一种

    谢谢

    该方法可以做到这一点:

    "ABCXYZ".Replace("X","XX")
    
    如果您事先不知道是哪个角色,可以使用:


    我认为,Replace将为您提供正确的字符串,但它不会提供替换的数量。要有效地做到这一点,可能需要这样做:

    char[] chars = string.ToCharArray();
    char[] destchars = new char[chars.Length * 2]; // Max possible string size
    int xidx = 0;
    
    // Replace the X's
    for(int i = 0; i < chars.Length; ++i)
    {
       if(chars[i] == 'X')
       {
          destchars[xidx] = destchars[xidx + 1] = chars[i];
          xidx += 2;
       }
       else
          destchars[xidx++] = chars[i];
    }
    
    int numberOfXs = (xidx - chars.Length);
    string newstr = new string(destchars, xidx);
    
    string x = new string("ABCXYZ".DoubleChar('X').ToArray());
    
    string x = new string("ABCXYZ".DoubleChar('X').DoubleChar('Y').ToArray());
    
    char[]chars=string.tocharray();
    char[]destchars=新字符[chars.Length*2];//最大可能字符串大小
    int-xidx=0;
    //更换X的
    对于(int i=0;i

    我手工输入,没有运行或调试,但你明白了。除了副本之外,使用双倍大小的缓冲区可能会有点内存不足。

    好的,您的新要求是不要使用现成的字符串方法,那么:

    var sbOut = new StringBuilder();
    string myString = "ABCXYZ";
    foreach (char c in myString) {
        sbOut.Append(c);
        if (c == 'X') {
            sbOut.Append(c);
        }
    }
    Console.WriteLine(sbOut.ToString());
    

    只是为了好玩,因为你说你不想使用内置的string方法,所以我写了一个扩展方法。我不知道这到底有多有效。它应该相当有效,但我不能保证

    public static class StringExt
    {
        public static IEnumerable<char> DoubleChar(this IEnumerable<char> inString,
                                                   char dupChar)
            {
    
             foreach (char c in inString)
             {
                yield return c;
                if (c == dupChar)
                {
                    yield return c;
                }
             }
        }
    }
    
    这还允许您将调用链接在一起,而无需制作字符串的多个副本。所以你可以这样做:

    char[] chars = string.ToCharArray();
    char[] destchars = new char[chars.Length * 2]; // Max possible string size
    int xidx = 0;
    
    // Replace the X's
    for(int i = 0; i < chars.Length; ++i)
    {
       if(chars[i] == 'X')
       {
          destchars[xidx] = destchars[xidx + 1] = chars[i];
          xidx += 2;
       }
       else
          destchars[xidx++] = chars[i];
    }
    
    int numberOfXs = (xidx - chars.Length);
    string newstr = new string(destchars, xidx);
    
    string x = new string("ABCXYZ".DoubleChar('X').ToArray());
    
    string x = new string("ABCXYZ".DoubleChar('X').DoubleChar('Y').ToArray());
    

    使用正则表达式和组引用;将替换文本中的组引用加倍。@Random832:感谢您推荐解决方案,但这里要避免任何现成的字符串函数这是家庭作业吗?无论如何,在这种情况下,请查看
    StringBuilder