String

  • Revision slug: JavaScript/Reference/Global_Objects/String
  • Revision title: String
  • Revision id: 54328
  • Created:
  • Creator: Roebot
  • Is current revision? No
  • Comment no wording changes

Revision Content

Summary

Creates an object that let's you work with a series of characters.

Syntax

new String()
new String(string)

String literals take the form:

'stringText'
"stringText"

Parameters

string
Any string.
stringText
Any series of characters that has been properly encoded.

Description

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

Character access

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"
The second way (treating the string as an array) is not part of the ECMAScript; it's a JavaScript feature.

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.

Comparing strings

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.

Properties

{{ Js_see_prototype("String", "Properties") }}

prototype
Allows the addition of properties to a String object.

{{ JSInherits("Function", "Properties", "caller", "constructor", "length", "name") }}

Methods

{{ Js_see_prototype("String", "Methods") }}

fromCharCode
Returns a string created by using the specified sequence of Unicode values.

{{ JSInherits("Function", "Methods", "apply", "call", "toSource", "toString", "valueOf") }} {{ JSInherits("Object", "Methods", "__defineGetter__", "__defineSetter__", "hasOwnProperty", "isPrototypeOf", "__lookupGetter__", "__lookupSetter__", "__noSuchMethod__", "propertyIsEnumerable", "unwatch", "watch") }}

String instances

{{ :en/Core_JavaScript_1.5_Reference/Global_Objects/String/prototype() }}

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/String", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Creates an object that let's you work with a series of characters.
</p>
<h3 name="Syntax"> Syntax </h3>
<p><code>new String()</code><br>
<code>new String(<i>string</i>)</code>
</p><p>String literals take the form:
</p><p><code><span class="nowiki">'</span><i>stringText</i><span class="nowiki">'</span></code><br>
<code>"<i>stringText</i>"</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>string</code>
</dt><dd> Any string.
</dd></dl>
<dl><dt> <code>stringText</code>
</dt><dd> Any series of characters that has been properly encoded.
</dd></dl>
<h3 name="Description">Description</h3>
<p>String objects are created by calling the constructor <code>new String()</code>. The <code>String</code> object wraps Javascript's string primitive data type with the methods described below. The global function <code>String()</code> can also be called without <code>new</code> in front to create a primitive string. String literals in JavaScript are primitive strings.</p>
<p>Because Javascript automatically converts between string primitives and String objects, you can call any of the methods of the <code>String</code> object on a string primitive. JavaScript automatically converts the string primitive to a temporary <code>String</code> object, calls the method, then discards the temporary <code>String</code> object. For example, you can use the <code>String.length</code> property on a string primitive created from a string literal:</p>
<pre class="brush: js">s_obj.length;       // 3
s_prim.length;      // 3
s_also_prim.length; // 3
'foo'.length;       // 3
"foo".length;       // 3
</pre>
<p>(A string literal can use single or double quotation marks.)</p>
<p>String objects can be converted to primitive strings with <code>String.valueOf()</code>.</p>
<p>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:</p>
<pre class="eval">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
</pre>
<h4 name="Character_access">Character access</h4>
<p>There are two ways to access an individual character in a string. The first is the <a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/String/charAt" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/charAt">charAt</a> method:</p>
<pre class="eval">return 'cat'.charAt(1); // returns "a"
</pre>
<p>The other way is to treat the string as an array, where each index corresponds to an individual character:</p>
<pre class="eval">return 'cat'[1]; // returns "a"
</pre>
<div class="note">The second way (treating the string as an array) is not part of the ECMAScript; it's a JavaScript feature.</div>
<p>In both cases, attempting to set an individual character won't work. Trying to set a character through <code>charAt</code> results in an error, while trying to set a character via indexing does not throw an error, but the string itself is unchanged.</p>
<h4 name="Comparing_strings">Comparing strings</h4>
<p>C developers have the <code>strcmp()</code> function for comparing strings. In JavaScript, you just use the <a href="/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators" title="en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators">less-than and greater-than operators</a>:</p>
<pre class="eval">var a = "a";
var b = "b";
if (a &lt; b) // true
  print(a + " is less than " + b);
else if (a &gt; b)
  print(a + " is greater than " + b);
else
  print(a + " and " + b + " are equal.");
</pre>
<p>A similar result can be achieved using the <a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/String/localeCompare" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/localeCompare">localeCompare</a> method inherited by <code>String</code> instances.</p><h3 name="Properties"> Properties </h3>
<p>{{ Js_see_prototype("String", "Properties") }}
</p>
<dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/String/prototype">prototype</a></dt><dd> Allows the addition of properties to a String object.
</dd></dl>
<p>{{ JSInherits("Function", "Properties", "caller", "constructor", "length", "name") }}
</p>
<h3 name="Methods"> Methods </h3>
<p>{{ Js_see_prototype("String", "Methods") }}
</p>
<dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/String/fromCharCode">fromCharCode</a></dt><dd> Returns a string created by using the specified sequence of Unicode values.
</dd></dl>
<p>{{ JSInherits("Function", "Methods", "apply", "call", "toSource", "toString", "valueOf") }}
{{ JSInherits("Object", "Methods", "__defineGetter__", "__defineSetter__", "hasOwnProperty", "isPrototypeOf", "__lookupGetter__", "__lookupSetter__", "__noSuchMethod__", "propertyIsEnumerable", "unwatch", "watch") }}
</p>
<h3 name="String_instances"> String instances </h3>
<p>{{ :en/Core_JavaScript_1.5_Reference/Global_Objects/String/prototype() }}
</p>
<div class="noinclude">
</div>
{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/String", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String" } ) }}
Revert to this revision