The String type is the reference type that corresponds to the string values. To create an object, a String constructor is used with a string being passed in, as shown below:

var stringObject = new String("Happy new year!");

The String type has an important property. That property is called length property and it may be used to retrieve the number of characters from a given string. For example:

var retLength = "Happy new year!";

alert(retLength.length);

As seen, this property counts all characters, including empty spaces.

The String type, same as Boolean and Number, besides traditional methods valueOf(), toLocaleString() and toString(), contains also a few additional methods. These methods are grouped and listed below.

String character methods

  • charAt();
  • charCodeAt();
  • fromCharCode();

The charAt() method returns the character at the position given within the apprentices, starting with 0 as the first character. The charCodeAt() is used to return the character's (at the given position) character code, instead of the character itself.

Example of character methods:

var text = "Happy new year";

alert(text.charAt(8)); //  "w"

alert(text.charCodeAt(8)); // "119"

The fromCharCode() method is the reverse operation from charCodeAt(). It returns a character's code and converts it into a string:

alert(String.fromCharCode(119)); // "w"

String manipulation methods

  • concat();
  • slice();
  • substr();
  • substring();

The concat() method is used to concatenate one or more strings to another, returning the concatenated string as result.

Example of concat() method:

var text = "Happy ";

alert(text.concat("new year")); // returns "Happy new year"

In addition to above mentioned explanation, it is important to know that the concat() method may accept more than one argument, in which case it just attaches it to the previous one. For instance:

alert(text.concat("new year!")); // returns "Happy new year!"

Other three methods, slice(), substr() and substring() are used to return a substring of a given string they are applied on. They may accept 1 or 2 arguments. The first argument is the position where capture of the substring begins, and the second one points at where the operation should stop. For slice() and substring(), the second argument is the position before which capture is stopped. For substr(), the second argument is the number of characters to return. naturally, if the second argument is omitted the return length will be the remaining length of the string.

Example of slice(), substr() and substring() methods:

var text = "Happy new year";

text.alert(slice(4)); // "y new year"

text.alert(substr(4)); // "y new year"

text.alert(substring(4)); // "y new year"

text.alert(slice(4,11)); // "y new y"

text.alert(substr(4,8)); // "y new ye"

text.alert(substring(4,11)); // y new y"

For the slice() method a negative argument is treated as the length of the string plus the negative argument. For the substr() method a negative first argument is treated as the length of the string plus the number, while the second number, if negative, is converted to 0. For the substring() method, all negative numbers are converted to 0. Examples of negative arguments can be found in the examples at the bottom of the page.

String location methods

  • indexOf();
  • lastIndexOf();

Both methods search a given string for a substring and return -1 if it hasn't been found. The difference between these two methods is that the indexOf() starts searching at the beginning of the string, while the lastIndexOf() starts at the end. Each method accepts an optional argument that indicates the position to start searching from.

var text = "Happy new year";

alert(text.indexOf("a")); // 1

alert(text.lastIndexOf("a")); // 12

String compare method

  • localeCompare();

The localeCompare() method compares one string to another and returns one of these values:

  • If the string comes alphabetically before the string argument, a negative number is returned (usually -1).
  • If the string is equal to the string argument, 0 is returned.
  • If the string comes alphabetically after the string argument, a positive number is returned (usually 1, depending on the implementation).

var text = "happy";

alert(text.localeCompare("fappy")); // 1

alert(text.localeCompare("happy")); // 0

alert(text.localeCompare("lappy")); // -1

String case methods

  • toLowerCase();
  • toLocaleLowerCase();
  • toUpperCase();
  • toLocaleUpperCase();

These methods are all used to convert case from lower to upper, and vice versa. The Locale methods are used to help conversation, according to locale rules, depending on the language settings.

var var text = "Happy new year";

alert(text.toUpperCase()); // "HAPPY NEW YEAR"

alert(text.toLowerCase()); // "happy new year"

String pattern matching methods

  • match();
  • search();
  • replace();
  • split();

The match() method has the same function as RegExp exec() method. It accepts a single argument which may be a regular expression, or a RegExp object.

var text = "happy new few years";

var pattern = /.ew/;

var matches = text.match(pattern);

alert(matches[0]); // new

The search() method returns the index of the first pattern occurrence in a string. It accepts only one argument, which is basically same as at match() method. If the search finds nothing, -1 is returned. This method starts searching at the beginning of the string and it always returns the first found result as shown in this example:

var text = "happy new few years";

alert(text.search(/ew/)); // 7

The replace() method accepts two arguments, the first one may be a RegExp object or a string, and the second a string or a function. If the first argument is a string, only the first occurrence will be replaced, otherwise to replace all instance of a substring is to provide a regular expression with the global flag specified.

var text = "happy new few years";

alert(text.replace("ew", "we")); // "happy nwe few years"

alert(text.replace(/ew/g, "we")); // "happy nwe fwe years"

ECMA-262 predicts a special character sequence to be used as a handy replacement for the second character, it is a string:

sequence replacement
$$ $
$& matching entire pattern (same as RegExp.lastMatch)
$' part of a string occuring before matches substring (same as RegExp.leftContext)
$` part of a string occuring after matched substring (same as RegExp.rightContext)
$n nth capture, where nn is a value 1-9
$nn nnth capture, where nn is a value 01-99

Example of a special sequence:

var text = "happy new few years";

alert(text.replace(/(.ew)/g, "happy ($1)")); // "happy happy (new) happy (few) years"

The split() method us used to separate a string into an array substrings based on a passed separator. The separator may be a string or a RegExp object as well. There is also an optional second argument that may be used as an array limit, so it does not exceed the given length.

var text = "happy new year";

alert (text.split(" ")); // ["happy", "new", "year"]

Example

The JavaScript String type examples (slice, substr, toUpperCase, replace):

 

›› go to examples ››