Method

Encoding


Description

Returns the text Encoding of the passed String.

Usage

result = sourceVariable.Encoding

Part

Type

Description

result

TextEncoding

The text encoding of str.

sourceVariable

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.

Sample code

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
t.Close
End If

Try
  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
  MessageBox(error.Message)
End Try

Compatibility

All project types on all supported operating systems.