1. Technology
Send to a Friend via Email

Advanced String Method Reference

Join the Discussion

Questions? Comments?

Related Resources

Tutorial Basic Methods

Methods for string manipulation were available in Javascript from the beginning but the methods originally available were somewhat primitive requiring substantial code to perform some basic manipulations. Javascript 1.2 introduced regular expression support and a selection of more advanced string manipulation methods.

  • charCodeAt(number) selects the single character at the specified position within the string and returns the ASCII code equivalent
  • concat(string2) concatenates string2 onto the end of the string
  • fromCharCode(code) converts the ascii code back into the character that it represents, this is a static method not associated with any string
  • localeCompare(string2) compares the string with string2 and returns -1, 0, or 1 depending on the sort sequence at the default locale
  • match(regexp) searches for substrings within the string that match the regular expression and returns them in an array
  • replace(regexp,string2) searches for substrings within the string that match the regular expression and replaces them with string2
  • search(regexp) returns true if a substring within the string matches the regular expression
  • split(char) splits the string at each occurrence of the specified character and puts them into an array (alternatively you can specify a regular expression instead of the char)
  • substr(start,end) gets the specified part of the string
  • toLocaleLowerCase() converts the string to lower case taking into account the default locale
  • toLocaleUpperCase() converts the string to upper case taking into account the default locale

These methods (particularly match, replace, split, and search) can make string manipulation much simpler provided that your visitors are using a browser recent enough to recognise them (which most browsers now do).

©2014 About.com. All rights reserved.