Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

String Manipulation

$
0
0

Hello, I am trying to clean-up a string, replace all special characters with "-" and then remove all duplicate hyphens except the first one. I need to create a function for this.

Original String:  "State cultural festivals & % ??? .. and music"

Clean string should look like this: State-cultural-festivals-and-music 

I got into cleaning all special characters but struggling to remove repeated hyphens except the first character. My current code is below. Any help would be appreciated.

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Dim MyString As String = "State cultural festivals & % ??? .. and music"
        MsgBox(Trim(CleanInput(MyString)))
    End Sub
    Public Function CleanInput(strIn As String) As String
        Try
            Dim returnStr As String
            returnStr = Regex.Replace(strIn, "[^\w\.@-]", "-")
            returnStr = Trim(returnStr)
            returnStr = returnStr.Replace(",", "-")
            returnStr = returnStr.Replace(".", "-")
            Return returnStr

        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
End Class

Current Output: 
State-cultural-festivals------------and-music

Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>