From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Returns the text encoding of the passed String.



Part Type Description
result TextEncoding The text encoding of str.
sourceVariable String The string whose TextEncoding will be returned.


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.


Var f As FolderItem
Var t As TextInputStream
Var source As String
Var enc As TextEncoding
f = FolderItem.ShowOpenFileDialog("text") // file type defined via the FileType class
If f <> Nil Then
t = TextInputStream.Open(f)
source = t.ReadAll
End If

enc = source.Encoding // This will be Encodings.UTF8

// If the file actually has text in a different encoding, then specify the
// encoding using DefineEncoding
source = source.DefineEncoding(Encodings.UTF16LE)

enc = source.Encoding // This is now Encodings.UTF16LE

Catch error As NilObjectException
End Try

See Also

TextConverter, TextEncoding, TextInputStream classes; String.ConvertEncoding, String.DefineEncoding, GetTextEncoding functions; Encodings module.