Method

# Encoding

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Returns the text <span class="title-ref">Encoding</span> of the passed `String</api/data_types/string>`.

## Usage

``` xojo
result = sourceVariable.Encoding
```

| Part           | Type                                                 | Description                                                                                                       |
|----------------|------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| result         | `TextEncoding</api/text/encoding_text/textencoding>` | The text encoding of *sourceVariable*.                                                                            |
| sourceVariable | `String</api/data_types/string>`                     | The `string</api/data_types/string>` whose `TextEncoding</api/text/encoding_text/textencoding>` will be returned. |

## Notes

This method does not attempt to "guess" the <span class="title-ref">Encoding</span> of a String. It only returns the <span class="title-ref">Encoding</span> of a String as it is known. Strings have a UTF-8 <span class="title-ref">Encoding</span> by default. If you load data of another <span class="title-ref">Encoding</span> into a String (from a file, perhaps), you will need to specify the <span class="title-ref">Encoding</span> using `DefineEncoding</api/text/encoding_text/defineencoding>`.

If the string's <span class="title-ref">Encoding</span> is unknown, <span class="title-ref">Encoding</span> returns `Nil</api/language/nil>`. Test whether the `TextEncoding</api/text/encoding_text/textencoding>` object is `Nil</api/language/nil>` or include an `Exception</api/exceptions/exception>` block if there is a chance the string's <span class="title-ref">Encoding</span> would not be known at runtime.

## Sample code

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

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`TextConverter</api/text/encoding_text/textconverter>`, `TextEncoding</api/text/encoding_text/textencoding>`, `TextInputStream</api/files/textinputstream>` classes; `String</api/data_types/string>`, `String</api/data_types/string>`, `GetTextEncoding</api/text/encoding_text/gettextencoding>` functions; `Encodings</api/text/encoding_text/encodings>` module.

</div>
