Android Code Snippet [B4X] Reading a UTF 8 file that might have BOM

Discussion in 'Code Snippets' started by Erel, Mar 22, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    UTF8 text files might include a BOM character. You can test it by using a decent text editor such as Notepad++.

    Always prefer to work with text files encoded with UTF8 without BOM.

    This code reads a UTF8 text file and removes the BOM character if needed:
    Code:
    Sub ReadUTF8FileMaybeWithBOM (Dir As String, Name As StringAs String
       
    Dim s As String = File.ReadString(Dir, Name)
       
    If s.StartsWith(Chr(0xFEFF)) Then
           s = s.SubString(
    1)
       
    End If
       
    Return s
    End Sub
     
    raphaelcno, fredo, asales and 8 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice