我想删除文本框中重复的字符

时间:2017-05-11 21:51:50

标签: vb.net

我正在使用VB.Net。 我需要删除文本框中的所有重复字符

例如:

myy naaaame isss Johnn 

my name is John

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

所以即便如此,我知道zilch关于VB.NET和RegEx在20分钟内发现了它:

Sub Main()
    Dim input As String = "myy naaaame isss Johnn"

    ' You need a regex group that matches any char: (.)
    ' ... and a back reference: \1
    ' ... and a count more than one: {1,}
    Dim rgx As New Regex("(.)\1{1,}")

    ' use the regex to Replace by the first char of the match group
    Dim output As String = rgx.Replace(input, New MatchEvaluator(Function(ByVal m)
                                                                     Return m.Value.First
                                                                 End Function))
End Sub