String
constructor
JS Home <<
JS Reference <<
String
String creation.
Description
In JavaScript strings are created using the constructor String
or created as string primitives.
- JavaScript will automatically convert primitives and
String
objects, so that it's possible to useString
object methods for primitive strings:- In contexts where a property lookup occurs JavaScript automatically performs the property lookup.
- In contexts where a method is to be called on a string primitive, JavaScript automatically wraps string primitives and calls the method.
Syntax
Signature | Description |
---|---|
var aString = new String(string); | Create a String object. |
String(string); | Create a string primitive. |
'stringText' | Create a string primitive. |
"stringText" | Create a string primitive. |
Parameters
Parameter | Description |
---|---|
string | Any string value. |
stringText | Any properly encoded set of characters. |
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type String . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the String function that created the prototype. |
length | A positive integer holding the number of elements within a string. |
n | A positive integer used to access a character between 0 and length -1. |
Class Methods
Class Method | Description |
---|---|
fromCharCode() | Returns a string created from the specified sequence of Unicode values. |
Instance Methods
By using methods of the String
object we can access individual characters of strings, split strings into arrays and many other things.
Instance Method | Description |
---|---|
Getter (Accessor) MethodsThese methods DO NOT modify the String object or string primitive |
|
charAt() | Returns the character at the specified zero based index. |
charCodeAt() | Returns the Unicode value of the character at the specified zero based index or NaN . |
concat() | Returns a new string from the concatenation of two or more strings. |
indexOf() | Returns the index value for the first occurence of the specified string from the specified starting index or -1 if not found. |
lastIndexOf() | Returns the index value for the last occurence of the specified string from the specified starting index or -1 if not found. |
localeCompare() | Returns a number dependant on the sort order of string comparison. |
match() | Returns match(es) of a string and regular expression comparison. |
replace() | Returns a new string containing some or all of the matches of a replaced pattern. |
search() | Tries to locate a match between a regular expression and this String object. |
slice() | Returns an extracted section of a string. |
substr() | Returns the characters of a section of an existing string. |
substring() | Returns a part of a string defined by parameters. |
toLocaleLowerCase() | Returns a copy of a string value converted to lower case, taking into account any specific case mappings for the locale. |
toLocaleUpperCase() | Returns a copy of a string value converted to upper case, taking into account any specific case mappings for the locale. |
toLowerCase() | Returns a copy of a string value converted to lower case. |
toString() | Returns a string representation of an object. |
toUpperCase() | Returns a copy of a string value converted to upper case. |
valueOf() | Returns the primitive value of a String object as a string data type. |
Setter (Mutator) MethodsThese methods DO modify the String object or string primitive |
|
None. |
None.
Usage
Any string primitives have full access to String
methods as JavaScript automaticially converts any string primitives to temporary String
objects when a String
method
is called on the string primitive. The following code shows an example of this.
// Create a String object.
aString = new String('happychappy');
alert('aString ' + aString.charAt(4)); // Will show value 'y' in alert box
// Create a string literal and call a String()
method on it.
bString = 'happychappy';
alert('aString ' + bString.charAt(4)); // Will show value 'y' in alert box
Related Tutorials
JavaScript Basic Tutorials - Lesson 8 - Strings