Replace consecutive characters with same single character

后端 未结 5 2603
执笔经年
执笔经年 2021-02-20 05:58

I was just wondering if there is a simple way of doing this. i.e. Replacing the occurrence of consecutive characters with the same character.

For eg: - if my string is \

相关标签:
5条回答
  • 2021-02-20 06:18

    word = "something likeeeee tttthhiiissss" re.sub(r"(.)\1+", r"\1",word)

    0 讨论(0)
  • 2021-02-20 06:25
    string myString = "something likeeeee tttthhiiissss";
    
    char prevChar = '';
    StringBuilder sb = new StringBuilder();
    foreach (char chr in myString)
    {
        if (chr != prevChar) {
            sb.Append(chr);
            prevChar = chr;
        }
    }
    
    0 讨论(0)
  • 2021-02-20 06:31
            Console.WriteLine("Enter any string");
            string str1, result="", str = Console.ReadLine();
            char [] array= str.ToCharArray();
            int i=0;
            for (i = 0; i < str.Length;i++ )
            {
              if ((i != (str.Length - 1)))
              { if (array[i] == array[i + 1]) 
                {
                    str1 = str.Trim(array[i]);
                }
                else
                {
                    result += array[i];
                }
              }
              else
              {
              result += array[i];
              }                    
            }
            Console.WriteLine(result);
    

    In this code the program ;

    1. will read the string as entered from user

    2.Convert the string in char Array using string.ToChar()

    1. The loop will run for each character in string

    2. each character stored in that particular position in array will be compared to the character stored in position one greater than that . And if the characters are found same the character stored in that particular array would be trimmed using .ToTrim()

    3. For last character the loop will show error of index out of bound as it would be the last position value of the array. That's why I used * if ((i != (str.Length - 1)))*

    6.The characters left after trimming are stored in result in concatenated form .

    0 讨论(0)
  • 2021-02-20 06:41

    How about:

    s = new string(s
         .Select((x, i) => new { x, i })
         .Where(x => x.i == s.Length - 1 || s[x.i + 1] != x.x)
         .Select(x => x.x)
         .ToArray());
    

    In english, we are creating a new string based on a char[] array. We construct that char[] array by applying a few LINQ operators:

    1. Select: Capture the index i along with the current character x.
    2. Filter out charaters that are not the same as the subsequent character
    3. Select the character x.x back out of the anonymous type x.
    4. Convert back to a char[] array so we can pass to constructor of string.
    0 讨论(0)
  • 2021-02-20 06:44

    This should do it:

    var regex = new Regex("(.)\\1+");
    var str = "something likeeeee!! tttthhiiissss";
    
    Console.WriteLine(regex.Replace(str, "$1")); // something like! this
    

    The regex will match any character (.) and \\1+ will match whatever was captured in the first group.

    0 讨论(0)
提交回复
热议问题