String is the built-in object corresponding to the primitive string data type. It contains a very large number of methods for string manipulation and examination, substring extraction, and even conversion of strings to marked-up HTML, though unfortunately not standards-oriented XHTML.
The String() constructor takes an optional argument that specifies its initial value:
var s = new String();
var headline = new String("Dewey Defeats Truman");
Because you can invoke String methods on primitive strings, programmers rarely create String objects in practice.
The only property of String is length, which indicates the number of characters in the string.
var s = "String fun in JavaScript";
var strlen = s.length;
// strlen is set to 24
The length property is automatically updated when the string changes and cannot be set by the programmer. In fact there is no way to manipulate a string directly. That is, String methods do not operate on their data “in place.” Any method that would change the value of the string, returns a string containing the result. If you want to change the value of the string, you must set the string equal to the result of the operation. For example, converting a string to uppercase with the toUpperCase() method would require the following syntax:
var s = "abc";
s = s.toUpperCase();
// s is now "ABC"
Invoking s.toUpperCase() without setting s equal to its result does not change the value of s. The following does not modify s:
var s = "abc";
s.toUpperCase();
// s is still "abc"
Other simple string manipulation methods such as toLowerCase() work in the same way; forgetting this fact is a common mistake made by new JavaScript programmers.
The String() constructor takes an optional argument that specifies its initial value:
var s = new String();
var headline = new String("Dewey Defeats Truman");
Because you can invoke String methods on primitive strings, programmers rarely create String objects in practice.
The only property of String is length, which indicates the number of characters in the string.
var s = "String fun in JavaScript";
var strlen = s.length;
// strlen is set to 24
The length property is automatically updated when the string changes and cannot be set by the programmer. In fact there is no way to manipulate a string directly. That is, String methods do not operate on their data “in place.” Any method that would change the value of the string, returns a string containing the result. If you want to change the value of the string, you must set the string equal to the result of the operation. For example, converting a string to uppercase with the toUpperCase() method would require the following syntax:
var s = "abc";
s = s.toUpperCase();
// s is now "ABC"
Invoking s.toUpperCase() without setting s equal to its result does not change the value of s. The following does not modify s:
var s = "abc";
s.toUpperCase();
// s is still "abc"
Other simple string manipulation methods such as toLowerCase() work in the same way; forgetting this fact is a common mistake made by new JavaScript programmers.