How do you remove repeated characters in a string

后端 未结 7 438
感情败类
感情败类 2020-12-09 11:44

I have a website which allows users to comment on photos. Of course, users leave comments like:

\'OMGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG

7条回答
  •  天命终不由人
    2020-12-09 12:23

    Regex would be overkill. Try this:

    public static string RemoveRepeatedChars(String input, int maxRepeat)
        {
            if(input.Length==0)return input;
    
            StringBuilder b = new StringBuilder;
            Char[] chars = input.ToCharArray();
            Char lastChar = chars[0];
            int repeat = 0;
            for(int i=1;i

提交回复
热议问题