String (Standard - JavaScript)
Represents a string.
Defined in
Standard (JavaScript)Usage
Strings can be formed as follows:- Literal string, formed by placing characters in double or single quotes.
- Variable whose value is a string primitive, formed by assigning a string literal, another string variable, or a string expression.
- Object of type String, formed by creating a new String object or assigning a String object.
Server-side JavaScript gives string primitives and literals access to String methods. You can follow a string primitive or literal with a period and the name of a String method. The interpreter wraps the primitive or literal in a temporary object.
A string object
is evaluated as a single string, while a string primitive or literal
is parsed. For example, "2 + 2"
is just that as an
object but the number 4 as a primitive or literal.
Index means the position of a character in a string. The first index is 0. The last index is the length of the string minus 1.
Examples
(1) In the following code, all three print statements result inPARIS MOSCOW TOKYO
.
Primitives and literals can use the String methods.function p(stuff) {
print("<<<" + stuff + ">>>");
}
var cities : String;
var cities2 : string;
try {
cities = new String("Paris Moscow Tokyo"); // String object
cities2 = "Paris Moscow Tokyo"; // string primitive
p(cities.toUpperCase());
p(cities2.toUpperCase()); // string primitive can use String methods
p('Paris Moscow Tokyo'.toUpperCase()); // string literal can use String methods
} catch(e) {
p("Error = " + e);
}
(2) This example demonstrates the difference between
evaluating a string literal (or primitive) and a String object.
requestScope.y = eval("2 + 2"); // 4
requestScope.x = eval(new String("2 + 2")) // 2 + 2