Encoding
From Xojo Documentation
Method
Returns the text encoding of the passed String.
Syntax
result=Encoding(str)
OR
result=str.Encoding
Part | Type | Description |
---|---|---|
result | TextEncoding | The text encoding of str. |
str | String | The string whose TextEncoding will be returned. |
Notes
This method does not attempt to "guess" the encoding of a String. It only returns the encoding of a String as it is known. Strings have a UTF-8 encoding by default. If you load data of another encoding into a String (from a file, perhaps), you will need to specify the encoding using DefineEncoding.
If the string's encoding is unknown, Encoding returns Nil. Test whether the TextEncoding object is Nil or include an Exception block if there is a chance the string's encoding would not be known at runtime.
Examples
Dim f As FolderItem
Dim t As TextInputStream
Dim s As String
Dim enc As TextEncoding
f = GetOpenFolderItem("text") // file type defined via the FileType class
If f <> Nil Then
t = TextInputStream.Open(f)
s = t.ReadAll
t.Close
End If
enc = s.Encoding // This will be Encodings.UTF8
// If the file actually has text in a different encoding, then specify the
// encoding using DefineEncoding
s = s.DefineEncoding(Encodings.UTF16LE)
enc = s.Encoding // This is now Encodings.UTF16LE
Exception err As NilObjectException
MsgBox(err.Message)
Dim t As TextInputStream
Dim s As String
Dim enc As TextEncoding
f = GetOpenFolderItem("text") // file type defined via the FileType class
If f <> Nil Then
t = TextInputStream.Open(f)
s = t.ReadAll
t.Close
End If
enc = s.Encoding // This will be Encodings.UTF8
// If the file actually has text in a different encoding, then specify the
// encoding using DefineEncoding
s = s.DefineEncoding(Encodings.UTF16LE)
enc = s.Encoding // This is now Encodings.UTF16LE
Exception err As NilObjectException
MsgBox(err.Message)
See Also
TextConverter, TextEncoding, TextInputStream classes; ConvertEncoding, DefineEncoding, GetTextEncoding functions; Encodings module.