Some Useful String Functions In JavaScript

27th December 2007

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.

  1. var str = 'abcdef';
  2. 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:

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

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

  1. var str = 'abcdef';
  2. 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.

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

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

  1. var str = 'abcdefa';
  2. 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.

  1. var str = 'abcdef';
  2. 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.

  1. var str = 'abcdef';
  2. 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.

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

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

  1. var str = 'abCDef';
  2. str.toUpperCase(); // returns 'ABCDEF'
  3. var str = 'abcdef';
  4. 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.

  1. var str = '1,2,3,4,5';
  2. 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.

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

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

  1. var str = 'abcdef';
  2. 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.

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

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

  1. var str = 'abCDeaf';
  2. alert(str.anchor('text')); // prints '<a name="text" id="text">abCDeaf</a>'
  3. alert(str.big()); // prints '<big>abCDeaf</big>'
  4. alert(str.blink()); // prints '<blink>abCDeaf</blink>'
  5. alert(str.bold()); // prints '<b>abCDeaf</b>'
  6. alert(str.fixed()); // prints '<tt>abCDeaf</tt>'
  7. alert(str.italics()); // prints '<i>abCDeaf</i>'
  8. alert(str.link('http://www.google.com')); // prints '<a href="http://www.google.com">abCDeaf</a>'
  9. alert(str.small()); // prints '<small>abCDeaf</small>'
  10. alert(str.strike()); // prints '<strike>abCDeaf</strike>'
  11. alert(str.sub()); // prints '<sub>abCDeaf</sub>'
  12. alert(str.sup()); // prints '<sup>abCDeaf</sup>'
  13. alert(str.fontcolor('#000000')); // prints <font color="#000000">abCDeaf</font>
  14. 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.