iOS Question Remove accents

JanPRO

Well-Known Member
Licensed User
Longtime User
Upvote 0

Andre Souza

Member
Licensed User
Probably there is a similar way in ios.
You can use replace to solve the issue i think, but i dont think it will be efficient as this code :

Lbl.text =Lbl.text.replace("accentletter","nonaccentletter")

Repeat this for every accent letter.
Thank You
 
Upvote 0

Andre Souza

Member
Licensed User
Hi,

try this:
B4X:
Sub RemoveAccents (S As String) As String
    Dim NaObj2 As NativeObject = S
    NaObj2 = NaObj2.RunMethod("dataUsingEncoding:allowLossyConversion:",Array(1,True))
  
    Dim NaObj As NativeObject
    Return NaObj.Initialize("NSString").RunMethod("alloc",Null).RunMethod("initWithData:encoding:",Array(NaObj2,1)).AsString
End Sub

Based on http://stackoverflow.com/questions/10932405/nsstring-easy-way-to-remove-utf-8-accents-from-a-string

Jan


Thanks! Your code works perfectly. Had done one manually which also works but your code is visually cleaner


B4X:
Sub IndexStr(Palavra As String, Posicao As Int) As String
    Return Palavra.SubString2(Posicao, Posicao +1)
End Sub

Sub RemoverAcentos(Palavra As String) As String
    Dim Acentos = "àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ"
    Dim SemAcentos = "aaeouaoaeioucuAAEOUAOAEIOUCU"
    Dim Posicao As Int
   
    Dim Letra As String
    Dim LetraSemAcento As String
   
    For i = 0 To Palavra.Length-1'
        Letra = IndexStr(Palavra, i)
        Posicao = Acentos.IndexOf(Letra)
       
        If Posicao > -1 Then
            LetraSemAcento = IndexStr(SemAcentos,Posicao)
            Palavra = Palavra.Replace(Letra, LetraSemAcento)
        End If
    Next'
   
    Return Palavra
   
End Sub
 
Upvote 0
Top