From Xojo Documentation

Revision as of 19:07, 20 December 2017 by PLefebvre (talk | contribs) (Examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Returns the text encoding of the passed String.



Part Type Description
result TextEncoding The text encoding of str.
str 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.


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
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

See Also

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