Ajax JavaScript CSS HTML 5 Gecko XUL Forum

Trying interactively methods of the String object in JavaScript

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 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);

 

The string object has a 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.

Lot of methods to handle a string of chars

Testing interactively String's methods

Enter an argument for the String constructor

position

string

color

string

regular expression

regular expression replacement

regular expression

position start position end

position start

position start position end

position start position end

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.
The charCodeAt method returns the Unicode value of a character.
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.
If the second argument is a function, the return value of this function replaces 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.
toLowerCase and toUpperCase
Convert a string respectively in upper and lower case. These methods have no parameters and do not modify the object.
© 2008-2011 Xul.fr