Begin Web Programming with PHP & MySQL

Advertisements



JavaScript String Object

A String is a sequence of characters, which is always enclosed with a single ('') or double quotes (""). String object wraps Javascript's string primitive data type with a number of string methods.

Properties of String Object
Property Description
constructor It returns a reference to the string function that created the object.
length It returns the length of the string
prototype It is used to add new properties and methods to a string object.

Methods of String Object
Method Description
charAt(position) It returns the character at the specified index.
charCodeAt(position) It returns the unicode of the character at a specified index in a string. The method returns a UTF-16 code.
concat(string1,string2,...) It joins two or more strings and returns the new string.
indexOf(search-string, position) It returns the position of the first occurrence of a value in a string and returns -1 if not found.
lastIndexOf(search-string, position) It returns the position of the last occurrence of a value in a string and returns -1 if not found.
localeCompare(string,position) It compares two strings in the current locale.
match(RegExp) It searches a string for a match using a given regular expression. It returns a matching array.
replace(search-string, replace-string) It is used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
search(RegExp) It is used to search for a match between a regular expression and a specified string.
slice(start-position, end-position) It is used to extract a section of a string based on specified starting and ending positions and returns a new string.
split(separator, limit) It splits a String into an array of strings by separating the string into substrings based on the specified separator. It splits until the specified limit, if given.
substr(start, length) It begins at a specified start position, and returns a specified number(length) of characters.
substring(start, end) It returns the characters in a string between start and end positions.
toLocaleLowerCase() It converts a string to lower case according to the current locale.
toLocaleUpperCase() It converts a string to upper case according to the current locale.
toLowerCase() It returns the string value converted to lowercase.
toString() It returns a string representing the specified object.
toUpperCase() It returns the string value converted to uppercase.
valueOf() It returns the primitive value of the specified string object.