VB.NET – Replacer les caractères spéciaux d’une chaine string

Dim chaine As String = “/-?:(),+àsdsdqd  áâãäåfdsg  dfgÀÁÂdfgà ÄÅéèêëËÊÉdfgAZAZAZFGGÈòóôõöÒÓÔÕÖùúûüÙÚÛÜìíîïÌÍÎÏÿýÝçÇ”
Dim chaine_final As String = “”
Dim non_autorise As Char() = “/-?:(),+àáâãäåÀÁÂÃÄÅéèêëËÊÉÈòóôõöÒÓÔÕÖùúûüÙÚÛÜìíîïÌÍÎÏÿýÝçÇ”.ToCharArray()
Dim autorise As Char() = “/-?:(),+aaaaaaAAAAAAeeeeEEEEoooooOOOOOuuuuUUUUiiiiIIIIyyYcC”.ToCharArray()

Dim i As Integer = 0
For Each s As String In chaine
For Each a As String In autorise
If i = non_autorise.Length Then
Exit For
End If
If s = non_autorise(i) Then
chaine = chaine.ToString.Replace(non_autorise(i), a)
End If
i += 1
Next
i = 0
Next

 

 

En faire une fonction :

Public Function remplacementCaractereSpeciaux(pChaineAModifier As String, pCaractereAutorise As String, pCaractereNonAutorise As String)

Dim chaine As String = pChaineAModifier
Dim non_autorise As Char() = pCaractereNonAutorise.ToCharArray()
Dim autorise As Char() = pCaractereAutorise.ToCharArray()

Dim i As Integer = 0
For Each s As String In chaine
For Each a As String In autorise
If i = non_autorise.Length Then
Exit For
End If
If s = non_autorise(i) Then
chaine = chaine.ToString.Replace(non_autorise(i), a)
End If
i += 1
Next
i = 0
Next

Return chaine

End Function

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s