The String object type allows the primitive string type to be accessed as objects. It allows manipulation and formatting of text strings and determination and location of sub-strings within strings.
Constructors
An instance of the String object can be created like this:
newString = new String({"stringLiteral"});The curly brackets are used to indicate that the string literal is optional.
String objects can also be created implicitly using string literals.
newString = "{stringLiteral}";Alternatively, if using the JScript scripting engine, the String type can be supplied to the variable:
JScript example:
newString : String = "{stringLiteral}";Properties
strVariable.length
"String Literal".length
length returns the length of a String object, an integer with the number of characters in the String object.
Index
Many of the methods of the String object refer to index on a string. The index is used to refer to a characters position within a string. If you have the string
0 1 2 3 4 5 6 7 8 9101112131415
"This is a string"the character a has index 8, because the index starts at 0 for first character. The index of the last character will always be 1 less than the string's length. This is similar to indexing in arrays.
Converting to String from Other Types
Often you want to convert variables of different types to string in order to use some of the string methods to manipulate on the variable's content. Most Objects will implement the .toString() method, and should, in general, be used in order to convert to string.
If for some reason, a variable is of an object that does not implement .toString(), you can instead convert the variable to a string by concatenating it win an empty string using the + operator:
variable+""
Since string has higher order of precedence over other types, this will convert the variable to type string.