The String object in JavaScript and interactive demo

The String object allows you to associate methods to strings. There are differences between a variable that is an instance of String, and a variable to which a string is assigned directly.
The constructor is also a mean to convert values to string objects.

The String converter may also be used to convert to string

The constructor accepts an argument that is a unique literal string or any object that you want to convert to a string.

The syntax is:

var x = new String("string");

Example

var x = new String("demo");

A number may be converted to a string with the String object:

var s = String(10) + 5
document.write(s)

This displays 105 because the values are strings and so are concatenated, not addedd:

A variable can be the argument of the constructor, as in any object:

var y = "demo";
var x = new String(y);
document.write(x);

The object is not interpreted by the eval function:
var x = new String("5 + 5");
var y = eval(x);
document.write(y);

This is not the case of a single variable:
var x = "5 + 5"; 
document.write(x);
var y = eval(x);
document.write(y);

length attribute

Length

Special HTML codes are not interpreted:

var x = new String(" ");
document.write(x.length);

Trying string with accents:

To length are also added attributes of Object.

Chars are accessed by an index

Like an array, we can indice a string to get the character at a given position:

var x = new String("demo");
document.write(x[2])   // should return m at 2 position from the beginning

The same result is obtained with the charAt method.

But is it also possible to replace a character at a position with its index?

x[1] = "Z"; 
document.write(x);   // should display dZmo

Unfortunately this does not work! We must therefore use other methods of the String object:

x = x.substr(0,1) + "Z" + x.substr(2);
document.write(x);

Or more generally:

x = x.substr(0,index) + "Z" + x.substr(index+1); 

chr and ord functions in JavaScript

The Unicode value of a character (provided by the ord function in other languages) is returned by this method:

x = y.charCodeAt(position); 

The position is 0 when y has only one character.

To replace chr and get the character corresponding to a Unicode value, write:

y = String.fromCharCode(x);

The argument is a Unicode value.

Methods of String

Interactive demo

Enter an argument for the String constructor

position

string

color

string

regular expression

regular expression replacement

regular expression

position start position end

separator

position start length

position start position end

If you use a string to be replaced in the replace() method, put it between quotes.

Description of the methods
x = y.anchor(name)
Creates an anchor from the string and a name given as parameter.
x = y.charAt(position)
Returns the character at a given position into given as argument from zero.
x = charCodeAt(position)
Returns the Unicode value of a character at the given position.
To retrieve the character from this code, use x = String.fromCharCode(x).
x = y.concat(str1 [, str2, ...)
Concatenate the string with one or more other strings and returns the new string.
y.fontcolor(couleur)
Displays string in the color argument given in digital form as #006600 or in text form as "blue".
The methods fontsize, bold, italic, strike, sub, sup, big, small, blink are other methods of presentation of String.
See corresponding CSS tags. The name is the same except for font-size.
x = y.indexOf(string [, position])
Returns the position in the original string of another string given as parameter. The function is case-sensitive.
Return -1 if the substring is not found. The position where stats the search is optional.
The LastIndexOf method returns the position from the right of the first occurrence of the string given as argument.
x = y.link(URL)
Creates a link with the string as anchor and the URL given as a parameter.
x = y.match(reg exp)
Return the result of a regular expression applied to the string. The method returns an array from occurrences found in the string.
x = y.replace(reg exp, string or function)
Replaces occurrences of the regular expression given in the first argument, by the string which is the second parameter, and then returns the result without changing the original object. You may use a string to be searched, but in this case only the first occurence will be replaced.
If the second argument is a function, the return value of this function replaces all occurrences found.
x = y.search(reg exp)
Search the regular expression (or a simple string) and returns the position. Returns -1 if not found.
x = y.slice(start [, end])
Extract parts of a string and returns the result without changing the original string. The parts to extract is defined by the parameters start and end, the end position not included. If end is omitted, it is the end of the string that is used as the second parameter.
If we give in parameters: 0 and length, thus it will return the same string.
The second argument can be negative and in this case it is subtracted from the length of the string.
x = y.split(separator [, limit])
Divide a string into substrings who are assigned to an array. The separator is the component used to identify the parties, usually a blank space or a comma, it will not be included in the array.
The optional argument "limit" indicates the maximum number of elements to be placed in the array.
x = y.substr(start, length)
x = y.substring(start, end)
These two functions extract a substring from a string.
Substring has the same parameters as slice.
Substr
corresponds to the function of the C language, parameters are the position of the substring and the number of characters.
The substring is returned without the original string is changed.
x = y.trim()
Removes spaces and special characters at the beginning and the end of the string. ECMAScript 1.8.
toLowerCase and toUpperCase
Convert a string respectively in upper and lower case. These methods have no parameters and do not modify the object.
© 2008-2014 Xul.fr