String

From Xojo Documentation

Data Type


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

Methods
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

Notes

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

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
MessageBox(letter)
Next

Converting a String to Text

You can convert a String to a Text by calling the ToText method.

Var s As String = "Hello"
Var t As Text = s.ToText

Converting Text to a String

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

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

Sample Code

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

Compatibility

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.