简体   繁体   中英

Replace consecutive characters with same single character

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

For eg: - if my string is "something likeeeee tttthhiiissss" then my final output should be "something like this".

The string can contain special characters too including space.

Can you guys suggest some simple way for doing this.

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.

string myString = "something likeeeee tttthhiiissss";

char prevChar = '';
StringBuilder sb = new StringBuilder();
foreach (char chr in myString)
{
    if (chr != prevChar) {
        sb.Append(chr);
        prevChar = chr;
    }
}

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 xx back out of the anonymous type x .
  4. Convert back to a char[] array so we can pass to constructor of string .
        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 .

word = "类似于eeee tttthhiiissss" re.sub(r"(.)\\1+", r"\\1",word)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM