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