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

前端之家收集整理的这篇文章主要介绍了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!!"
原文链接:https://www.f2er.com/csharp/94229.html

猜你在找的C#相关文章