From Xojo Documentation

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

Data Type

A String is an intrinsic data type. It is a series of numeric or alphabetic characters enclosed in quotes.

Asc IsEmpty Right
AscByte LastField RightBytes
BeginsWith Left Split
Bytes LeftBytes SplitBytes
Characters Length Titlecase
Codepoints Lowercase ToArray
Compare Middle ToDouble
ConvertEncoding MiddleBytes ToInt64
CountFields NthField ToInteger
DefineEncoding Replace Trim
Encoding ReplaceAll TrimLeft
EndsWith ReplaceAllBytes TrimRight
IndexOf ReplaceBytes Uppercase
IndexOfBytes ReplaceLineEndings Val
Shared Methods
ChrByte FromArray


Any kind of alphabetic or numeric information can be stored as a string. "Jean Marie", "3/17/98", "45.90" are all examples of strings. When you place quotes around information in your code, you are indicating the data as just a series of characters and nothing more. The maximum length of a string is limited only by available memory. The default value of a String is "".

All computers use encoding schemes to store character strings as a series of bytes. The oldest and most familiar encoding scheme is the ASCII encoding. It defines character codes only for values 0-127. These values include only the upper and lowercase English alphabet, numbers, some symbols, and invisible control codes used in early computers.

Many extensions to ASCII have been introduced which handle additional symbols, accented characters, non-Roman alphabets, and so forth. In particular, the Unicode encoding is designed to handle any language and a mixture of languages in the same string. Two different Unicode formats are supported: UTF-8 and UTF-16. Your constants, string literals, and so forth are nearly always stored internally using UTF-8 encoding.

If the strings you work with are created, saved, and read within your code, you shouldn't have to worry about encoding issues because the encoding is stored along with the content of the string.

Since character codes above 127 represent different characters in different encoding schemes, it is important that you understand the encoding that is used for strings that were generated outside of your app. When you read in a text string using the TextInputStream class, set the Encoding property to the correct encoding or use the optional Encoding parameter of the Read, ReadLine, or ReadAll methods. You can determine the encoding of a string using the Encoding function. If you need to save string data in a particular encoding, use the ConvertEncoding function. Specify the desired encoding using the Encodings module.

If you need to get a character that corresponds to the value of a character code, use the Chr function only if it is an ASCII code. In other cases, it is best to use the Chr method of the TextEncoding class. This enables you to specify both the encoding scheme and the desired character code.

The VarType function returns 8 when passed a String.

Iterating Through Characters

If you need to access each character of a string but don't need to know the index of that character, you can use an iterator:

Var wizard As String = "Gandalf"
For Each letter As String In wizard.Characters

Converting From the Text Type

Text values automatically convert to String (with UTF8 encoding) when assigned:

Var t As Text = "Hello, World!"
Var s As String = t

Empty does not equal Nil

Despite an empty string contains nothing, such string is not equivalent to Nil. Nil represents a non-existing object, whereas an empty string is an existing object even if it is empty.

Including Double Quotes

If you need to include a double quote mark inside your string literal, you can do it by typing two consecutive double quote marks. For example, a sentence like He told me "yes" can be typed in as a string literal as:

Var s As String = "He told me ""yes"""

Following the same logic, the literal """" actually represents a single double quote character (the first and last double quotes denote a string literal and the 2 consecutive double quotes in-between represents a double quote character).

The ampersand (&) character

Be aware that a single ampersand (&) character will often be interpreted as a keyboard shortcut if you are using your string literal inside the user interface, e.g. if you use your string literal as a button caption, a menu item… This behavior is aimed at implementing keyboard shortcuts (a.k.a. keyboard accelerators) on Microsoft Windows operating system. As a consequence, you may need to use "&&" instead of a single "&" character in some cases.

Sample Code

Var s As String
s = "Hello, World!"


All project types on all supported operating systems.

See Also

-, +, *, /, <, <=, =, >=, >, <> operators; &u literal; ConvertEncoding, IsNumeric, VarType functions; Var, Static statements; Encodings module; TextEncoding, TextInputStream classes; Boolean, CFStringRef, Color, CString, Currency, Double, Integer, PString, Variant, WString data types.