Skip to content

Instantly share code, notes, and snippets.

@tadeubdev
Last active March 2, 2017 12:42
Show Gist options
  • Select an option

  • Save tadeubdev/24e27b5c648be3fe61f879d19381fb4e to your computer and use it in GitHub Desktop.

Select an option

Save tadeubdev/24e27b5c648be3fe61f879d19381fb4e to your computer and use it in GitHub Desktop.
Prepare string, remove double spaces, special caracters
Imports System.Globalization
Imports System.Text
Imports System.Text.RegularExpressions
Public Class StringHelper
Shared normalizedString As String
Shared stringBuilder As New StringBuilder
Shared Response As String
Shared i As Integer
Shared c As Char
Shared Explode() As String
Shared Function PrepareString(STR As String)
Response = RemoveDiacritics(STR)
Response = CleanString(Response)
Response = ToUpperFirst(Response)
Return Response
End Function
Shared Function RemoveDiacritics(ByVal STR As String)
normalizedString = STR.Normalize(NormalizationForm.FormD)
For i = 0 To normalizedString.Length - 1
c = normalizedString(i)
If CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark Then
stringBuilder.Append(c)
End If
Next
STR = stringBuilder.ToString()
stringBuilder.Clear()
Return STR
End Function
Private Shared Function CleanString(STR As String)
'aceita somente alguns caracteres
STR = Regex.Replace(STR, "[^A-Za-z0-9+\- ()]", "")
'remover espaços a mais
Return Regex.Replace(STR, "\s+", " ")
End Function
Shared Function ToUpperFirst(STR As String)
STR = STR.ToUpper
Explode = STR.Split("-")
If Explode.Count > 1 Then
Explode(Explode.Count - 1) = Capitalize(Explode(Explode.Count - 1))
End If
Return String.Join("-", Explode)
End Function
Private Shared Function Capitalize(v As String) As String
Dim Explode = v.Split(" ")
For Each Word In Explode
Dim Index As Integer = Array.IndexOf(Explode, Word)
Dim Value As String = Word
If Word.ToString.Length > 2 Then
Value = Word.Substring(0, 1).ToUpper
Value &= Word.Substring(1, Word.ToString.Length - 1).ToLower
End If
Explode(Index) = Value
Next
Return String.Join(" ", Explode)
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment