Some Useful String Functions In JavaScript

Here are a few of the built in JavaScript functions available.

To get the length of a string use the length variable. This returns the number of characters in a string.

var str = 'abcdef';
str.length; // returns 6

The charAt function will return the character at the point specified in the parameter. So to get the first character of a string use:

var str = 'abcdef';
str.charAt(0); // returns a

To get the last character of a string use a combination of charAt and length.

var str = 'abcdef';
str.charAt(str.length - 1); // returns 'f'

To get the position of a character or group of characters in a string use the indexOf function. The function returns -1 if the string is not found.

var str = 'abcdef';
str.indexOf('a'); // returns 0

There is also the function lastIndexOf() which returns the last occurrence of a string in a string.

var str = 'abcdefa';
 str.lastIndexOf("a");// returns 6

To get a portion of a string use the substring() function. The first parameter is the character to start from, and the second parameter is the character to end at.

var str = 'abcdef';
str.substring(1, 4); // returns 'bcd'

This can also be done with the substr() function, which has the same first parameter as substring(), but the second parameter is the length of the string to be returned. Note that different browsers have different levels of support for each function.

var str = 'abcdef';
str.substr(1,4); // return 'bcde'

An alternate method of slicing a string is to use the slice function. The two parameters it takes are the same as the substring() function.

var str = 'abcdef';
str.slice(1,4); // returns 'bcd'

To convert a string to a different case you can use the toUpperCase() or toLowerCase() functions.

var str = 'abCDef';
str.toUpperCase();  // returns 'ABCDEF'
var str = 'abcdef';
str.toLowerCase();  // returns 'abcdef'

To convert a string into an array you can use the split() function. The parameter it takes is the value to split the string by.

var str = '1,2,3,4,5';
var arr = str.split(',');

To use regular expressions on a string the match(), replace() and search() functions exist. The parameters they take are the regular expression string, with replace a second parameter is needed which is the string to replace the regular expression match.

match() returns a string containing the matched value in this case 'a' is printed as this is the first one to be found.

var str = 'abcdef';
alert(str.match(/a/)); // prints a

replace() will swap any regular expression matches with the value in the second parameter.

var str = 'abcdef';
alert(str.replace(/a/,'t')); // prints 'tbcdef'

search() will find the position of the first regular expression match in a string. The following example prints off 4 as this is the position of the first 'e' found in the string.

var str = 'abcdef';
alert(str.search('e')); // prints 4

Return str with certain formatting imposed upon it. Here are some examples.

var str = 'abCDeaf';
alert(str.anchor('text')); // prints '<a name="text">abCDeaf</a>'
alert(str.big()); // prints '<big>abCDeaf</big>'
alert(str.blink()); // prints '<blink>abCDeaf</blink>'
alert(str.bold()); // prints '<b>abCDeaf</b>'
alert(str.fixed()); // prints '<tt>abCDeaf</tt>'
alert(str.italics()); // prints '<i>abCDeaf</i>'
alert(str.link('http://www.google.com')); // prints '<a href="http://www.google.com">abCDeaf</a>'
alert(str.small()); // prints '<small>abCDeaf</small>'
alert(str.strike()); // prints '<strike>abCDeaf</strike>'
alert(str.sub()); // prints '<sub>abCDeaf</sub>'
alert(str.sup()); // prints '<sup>abCDeaf</sup>'
alert(str.fontcolor('#000000')); // prints <font color="#000000">abCDeaf</font>
alert(str.fontsize(6)); // prints <font size="6">abCDeaf</font>

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
8 + 12 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.