Encoding

From Xojo Documentation

Revision as of 19:16, 19 November 2009 by 10.0.1.5 (talk) (Examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Description

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

Use the Encoding function to determine the encoding of a string. This is useful for text that you have read in from an external source, for example, with TextInputStream and you don't know the encoding. For text that is generated within REALbasic, the encoding is known to REALbasic.

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

The following example gets the encoding of the text stored in the local variable, s. The Exception block handles the call to one of the TextEncoding object's properties if the encoding cannot be determined or the file cannot be opened.

Dim s As String
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
MsgBox err.message

See Also

TextConverterTextEncoding, TextInputStream classes; ConvertEncoding, DefineEncoding, GetTextEncoding functions; Encodings module.