C# · 12月 30, 2021

c# – 如何删除字符串中的重复字符

我有一个网站允许用户评论照片.
当然,用户会发表如下评论:

‘OMGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG!!!!!!!!!!!!!!!’

要么

‘YOU SUCCCCCCCCCCCCCCCCCKKKKKKKKKKKKKKKKKK’

你懂了.

基本上,我想通过删除至少大部分超重复字符来缩短这些评论.
我确定有一种方法可以使用Regex …,但是不能弄清楚.

有任何想法吗?

解决方法 请记住,英语使用双字母通常你可能不想盲目地消除它们.这是一个正则表达式,将摆脱任何超越双重的东西. Regex r = new Regex(“(.)(?<=\\1\\1\\1)”,RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);var x = r.Replace(“YOU SUCCCCCCCCCCCCCCCCCKKKKKKKKKKKKKKKKKK”,String.Empty);// x = “YOU SUCCKK”var y = r.Replace(“OMGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG!!!!!!!!!!!!!!!”,String.Empty);// y = “OMGG!!”