Revision 54323 of String

  • Revision slug: JavaScript/Reference/Global_Objects/String
  • Revision title: String
  • Revision id: 54323
  • Created:
  • Creator: Andr3w
  • Is current revision? No
  • Comment Undid my own dumb edit (sorry)

Revision Content

This page documents the use of the String object itself and its use as a constructor. For a list of properties and methods inherited by String instances, see String.prototype.

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():

s_obj = new String("foo"); // creates a String object

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:

s_prim = String("bar"); // creates a primitive string

Literal strings in Javascript source code create primitive strings:

s_also_prim = "foo"; // creates a primitive string

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

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

prototype
Allows the addition of properties to a String object.

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

Methods

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

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

{{ wiki.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>
<dl><dd> <i><small style="line-height: 100%;">This page documents the use of the <code>String</code> object itself and its use as a constructor. For a list of properties and methods inherited by <code>String</code> instances, see <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/String/prototype">String.prototype</a>.</small></i>
</dd></dl>
<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="plain">'</span><i>stringText</i><span class="plain">'</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>:
</p>
<pre class="eval">s_obj = new String("foo"); // creates a String object
</pre>
<p>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:
</p>
<pre class="eval">s_prim = String("bar"); // creates a primitive string
</pre>
<p>Literal strings in Javascript source code create primitive strings:
</p>
<pre class="eval">s_also_prim = "foo"; // creates a primitive string
</pre>
<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="eval">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="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">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">localeCompare</a> method inherited by <code>String</code> instances.
</p>
<h3 name="Properties"> Properties </h3>
<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>{{template.JSInherits("Function", "Properties", "caller", "constructor", "length", "name")}}
</p>
<h3 name="Methods"> Methods </h3>
<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>{{template.JSInherits("Function", "Methods", "apply", "call", "toSource", "toString", "valueOf")}}
{{template.JSInherits("Object", "Methods", "__defineGetter__", "__defineSetter__", "hasOwnProperty", "isPrototypeOf", "__lookupGetter__", "__lookupSetter__", "__noSuchMethod__", "propertyIsEnumerable", "unwatch", "watch")}}
</p>
<div class="noinclude">
</div>
{{ wiki.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