| ★ wanayoo — archive 1999 http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String | Nouvelle recherche | Portail wanayoo |
String objects are created by calling the constructor new String(). The String object wraps Javascript's string primitive data type with the methods described below. The global function String() can also be called without new in front to create a primitive string. String literals in JavaScript are primitive strings.
Because Javascript automatically converts between string primitives and String objects, you can call any of the methods of the String object on a string primitive. JavaScript automatically converts the string primitive to a temporary String object, calls the method, then discards the temporary String object. For example, you can use the String.length property on a string primitive created from a string literal:
s_obj.length; // 3 s_prim.length; // 3 s_also_prim.length; // 3 'foo'.length; // 3 "foo".length; // 3
(A string literal can use single or double quotation marks.)
String objects can be converted to primitive strings with String.valueOf().
String primitives and String objects give different results when evaluated as Javascript. Primitives are treated as source code; String objects are treated as a character sequence object. For example:
s1 = "2 + 2"; // creates a string primitive
s2 = new String("2 + 2"); // creates a String object
eval(s1); // returns the number 4
eval(s2); // returns the string "2 + 2"
eval(s2.valueOf()); // returns the number 4
There are two ways to access an individual character in a string. The first is the charAt method:
return 'cat'.charAt(1); // returns "a"
The other way is to treat the string as an array, where each index corresponds to an individual character:
return 'cat'[1]; // returns "a"
In both cases, attempting to set an individual character won't work. Trying to set a character through charAt results in an error, while trying to set a character via indexing does not throw an error, but the string itself is unchanged.
C developers have the strcmp() function for comparing strings. In JavaScript, you just use the less-than and greater-than operators:
var a = "a"; var b = "b"; if (a < b) // true print(a + " is less than " + b); else if (a > b) print(a + " is greater than " + b); else print(a + " and " + b + " are equal.");
A similar result can be achieved using the localeCompare method inherited by String instances.
For properties inherited by String instances, see Properties of String instances.
Properties inherited from Function.prototype
caller, constructor, length, name
For methods inherited by String instances, see Methods of String instances.
Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch
All String instances inherit from String.prototype. Changes to the String prototype object are propagated to all String instances.
=== Properties ===
"").
Non-standard
<a name="name"> (hypertext target)
<blink>
<font color="color">
<font size="size">
<a href=/old?u=http%3A%2F%2Fdeveloper.mozilla.org%2Fen%2FCore_JavaScript_1.5_Reference%2FGlobal_Objects%2F%26quot%3B%3Ci&y=1999>url"> (link to URL)
These methods are of limited use, as they provide only a subset of the available HTML tags and attributes.
Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch
repeat method The following example creates a method, str_rep, and uses the statement String.prototype.repeat = str_rep to add the method to all String objects. All String instances then have that method, even objects already created. The example then creates an alternate method and overrides the previous method in one of the String objects using the statement s1.repeat = fake_rep. The str_rep method of the remaining String objects is not altered.
var s1 = new String("a");
var s2 = new String("b");
var s3 = new String("c");
// Create a repeat-string-N-times method for all String objects
function str_rep(n) {
var s = "", t = this.toString();
while (--n >= 0) {
s += t
}
return s;
}
String.prototype.repeat = str_rep;
s1a=s1.repeat(3); // returns "aaa"
s2a=s2.repeat(5); // returns "bbbbb"
s3a=s3.repeat(2); // returns "cc"
// Create an alternate method and assign it to only one String variable
function fake_rep(n) {
return "repeat " + this + " " + n + " times.";
}
s1.repeat = fake_rep
s1b=s1.repeat(1); // returns "repeat a 1 times."
s2b=s2.repeat(4); // returns "bbbb"
s3b=s3.repeat(6); // returns "cccccc"
The function in this example also works on String objects not created with the String constructor. The following code returns "zzz".
"z".repeat(3);
Page last modified 06:51, 20 Aug 2008 by Roebot