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

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:
B4X:
Sub ReadUTF8FileMaybeWithBOM (Dir As String, Name As String) As 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
 
Top