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 LTrim Replace
AscB Left ReplaceAll
CDbl LeftB ReplaceAllB
CLong Len Right
ConvertEncoding LenB RightB
CountFields Lowercase Split
CountFieldsB Mid SplitB
DefineEncoding MidB Titlecase
Encoding NthField Trim
InStr NthFieldB Uppercase
InStrB RTrim Val

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.

Converting a String to Text

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

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

Using Global Functions

You can also use most of the String methods (excluding ToText) as a global function. For example:

Dim s As String = "HELLO"
Dim result As String
result = Lowercase(s) ' global function
result = s.Lowercase ' standard function

Converting a String to Text

You can convert a String to a Text by calling its ToText method:

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

Converting Text to a String

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

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

Sample Code

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

See Also

-, +, *, /, <, <=, =, >=, >, <>, Asc, AscB, Chr, ChrB, ConvertEncoding, CStr, Encoding, InStr, InStrB, IsNumeric, Left, LeftB, Len, LenB, Lowercase, LTrim, Mid, MidB, NthField, Replace, ReplaceAll, Right, RightB, RTrim, Str, StrComp, Titlecase, Trim, Uppercase, Val, VarType functions; Dim, Static statements; Encodings module; TextEncoding, TextInputStream classes; Boolean, Byte, CFStringRef, Color, CString, Currency, Delegate, Double, Int16, Int32, Int64, Int8, Integer, OSType, PString, Ptr, Short, Single, String, UInt16, UInt32, UInt64, UInt8, Variant, WindowPtr, WString data types.