JavaScript String Class



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site







Quick Reference for the String Class



The String Class Reference Table
Native class
Inherits From Object
Constants Properties Methods Constructors
Syntax Arguments Returns
 
None length Class Methods
String.fromCharCode( )
Instance Methods
charAt()
charCodeAt()
concat()
indexOf()
lastIndexOf()
localeCompare()
match()
replace()
search()
slice()
split()
substr()
substring()
toLowerCase()
toString()
toUpperCase()
valueOf()
new String(string) string A new string object




String Class Description

The String class provides methods for operating on primitive string values. All String methods are accessors as opposed to mutators. These methods all return values but do not change the original string object.





String Class Constants

There are no explicit constants for the class.





String Class Instance Properties

The String class does inherit the following properties from the base Object class.





String Class Methods

All methods of the String class are instance (non-static) methods with the exception of the fromCharCode method which is a static (class) method.





String Class Instance Methods

The String class does inherit some methods from the base Object class. The explicit methods toString and valueOf of the String class will override the corresponding method of the base Object class.

The string methods all have the following commonalities:

string sob        T   h   e       s   o   b      s   t   r   i   n   g
relative   1   2   3   4   5   6   7   8   9  10  11  12  13  14
index   0   1   2   3   4   5   6   7   8   9  10  11  12  13

The Methods for the String Class
Method
Syntax / Arguments
Returns Description / Example
 
Method: charAt()

Syntax:
sob.charAt(n)

Argument: n
n = index of desired character
returns the nth character of the string The charAt( ) method behaves as follows:
  • returns the nth character of the String object as designated by the index that is specified as the argument
  • the method will return an empty string if the argument (index number) is not between zero and length - 1
  • the method will return an empty string if the argument is a negative value
  • the returned character is a string of length one

Example:
var sob = new String("The sob string"); sob.charAt(6); // returns b
Method: charCodeAt()

Syntax:
sob.charCodeAt(n)

Argument: n
n = index of desired character
returns the Unicode encoding of the nth character of the string The charCodeAt( ) method behaves as follows:
  • returns the nth character (with Unicode encoding) of the String object as designated by the index that is specified as the argument
  • the method will return NaN if the argument (index number) is not between zero and length - 1
  • the method will return NaN if the argument is a negative value
  • the returned value is a 16-bit integer between 0 and 65535

Example:
var sob = new String("The sob string"); sob.charCodeAt(6); // returns 98
Method: concat()

Syntax:
sob.concat(value,....)

Argument: value,....
value: one or more values to be concatenated to the string object
returns a new string with all values concatenated together The concat( ) method behaves as follows:
  • this method returns the original string of the object with the argument values appended to it
  • the values of the argument are appended in the order they are listed in the argument
  • any values in the argument list will be converted to a string if they are not already in the string format
  • the string of the original object is not altered
  • the addition operator may be a better alternative for concatenation

Example:
var sob1 = new String("The "); var s3 = " String"; sob1.concat("sob", s3);//returns "The sob String"
Method: String.fromCharCode()

Syntax:
String.fromCharCode(u1, u2,....)

Argument: u1, u2,....
one or more unicode encoding's to be concatenated to the string object
returns a new string with all values concatenated together The static method String.fromCharCode( ) method behaves as follows:
  • String.fromCharCode() is a class method
  • the values of the argument are appended in the order they are listed in the argument
  • this method returns a string where the unicode encoding is converted to the corresponding ASCII characters

Example:
String.fromCharCode(115, 111, 98);//returns "sob"
Method: indexOf()

Syntax:
sob.indexOf(substring)
sob.indexOf(substring, start)

Argument:
substring: a substring representing the search criteria
start: the search starting point
returns the index of the first occurrence of substring The indexOf( ) method behaves as follows:
  • this method returns the position (index) of the first occurrence of the first character of the substring argument
  • the method return -1 when no occurrence is found
  • the first parameter (substring) must be specified as a string
  • the optional start parameter will govern the starting position of the search; when the start parameter is not specified, the method automatically starts at index zero
  • the search is from left to right
  • the start parameter represents the index value of the start search character; the index values can be from zero to string.length - 1
  • the string of the original object is not altered

Example:
var sob = new String("The sob string"); sob.indexOf("sob", 1);// returns 4 sob.indexOf("sob", 7);// returns -1 sob.indexOf("sob");// returns 4
Method: lastIndexOf()

Syntax:
sob.lastIndexOf(substring)
sob.lastIndexOf(substring, start)

Argument:
substring: a substring representing the search criteria
start: the search starting point
returns the index of the last occurrence of substring The lastIndexOf( ) method behaves as follows:
  • this method returns the position (index) of the last occurrence of the first character of the substring argument
  • the method returns -1 when no occurrence is found
  • the search begins at the end of the string or at the position specified by the start parameter
  • the search precedes from right to left
  • the first parameter (substring) must be specified as a string
  • the start parameter represents the index value of the start search character; the index values can be from zero to string.length - 1
  • the string of the original object is not altered

Example:
var sob = new String("The sob string"); sob.lastIndexOf("sob", 1);// returns -1 sob.lastIndexOf("sob", 7);// returns 4 sob.lastIndexOf("sob");// returns 4 //recall the search is right to left
Method: localeCompare()

Syntax:
sob.localeCompare(string2)

Argument: string2
string2: a string to be compared to the string object
returns a number that indicates the compare result The localeCompare( ) method behaves as follows:
  • this method compares the string specified as the argument with the primitive value of the string object
  • this method returns a number that indicates the result of the string comparison:
    • returns zero when the strings compare equal
    • returns less than zero when the string of the object is less than string2
    • returns greater than zero when the string of the object is greater than string2
  • the ECMAScript standard specifies that this method uses the collating order intrinsic to the local operating system

Example:
var sob = new String("1st street apt#3"); var sob1 = "1st street apt#3"; sob.localeCompare(sob1);// returns 0
Method: match()

Syntax:
sob.match(regexp)

Argument: regexp
regexp: a regular expression pattern
returns the results of the match in the form of an array The match( ) method behaves as follows:
  • this method is supportive of regular expressions
  • this method returns an array that reflects the results of the match of the string object to the regular expression argument
  • if there is not a match, the method returns null
  • if regular expression has modifying a "g" flag, the match performed is global (searches the string for all matches); each of the matched occurrences are returned in the array
  • if regular expression does not have a "g", the match performed is a single match and the array element 0 contains the matched text
  • if the argument is not a regular expression, it will be converted to one using the RegExp() constructor

Example:
var sob = new String("1st street apt#3"); sob.match(/\d+/g);// returns 1,3
Method: replace()

Syntax:
sob.replace(regexp, replacement)
sob.replace(literal, replacement)

Argument: regexp
regexp: a regular expression pattern to be replaced
literal: a substring to be replaced
replacement: the replacement string
returns a new string with replacements The replace( ) method behaves as follows:
  • this method is supportive of regular expressions
  • the first argument is the matching criteria and the second argument is the replacement criteria
  • this method returns a new string with the replacement from the second argument when a match occurred as specified by the first argument
  • if regular expression has a "g", the match performed is global (searches the string for all matches); otherwise, the match performed is a single match
  • when the first argument is not a regular expression, it will behave as a string literal and acts as a substring in the match
  • the replacement argument can be specified as:
    • a string (a simple replacement string)
    • a string with the $ character; behavior is altered as:
      • $1,$2,...$99:
      each $ relates to the set of possible parenthesized sub expression within regular expression; up to 99 allowed
      • $&:
      the substring that matched regular expression
      • $:
      the text to the left of the matched substring
      • $':
      the text to the right of the matched substring
      • $$:
      a literal dollar sign
    • a function: the function is invoked for each match and the function returns the replacement string

Example:
var sob = new String("1st street apt#3"); sob.replace(/1st/g, "first"); // returns "first street apt#3"
Method: slice()

Syntax:
sob.slice(start, end)

Arguments: start, end
start: the string index at where the slice is to begin
end: the string index at where the slice is to end
slice() returns a new string that represents the slice The slice( ) method behaves as follows:
  • slice() returns a new string that contains the characters sliced from the original string
  • the returned string starts (first character) with the character specified with the start argument
  • the returned string includes all subsequent characters up to but not including the character specified by the end argument
  • the start and end arguments can be negative numbers
  • when the arguments are negative, the positions (start and/or end) are measured from the end of the originating string
  • with negative arguments, they only affect the positioning of the slice, the slice is still performed from left to right (it is not a backwards slice)
  • when the end argument is not specified, the slice will include all characters from the starting position to the end of the originating string
  • slice() differs from substring() and substr():
    • from substring(): substring() does not allow for negative arguments
    • from substr(): substr() has a length specified as the second argument
Example:
var sob = new String("1st street apt#3"); sob.slice(4,10);// returns street
Method: split()

Syntax:
sob.split(delimiter, length)
sob.split(delimiter)

Arguments:
delimiter: a regexp or string
length: optional - the maximum length of the array
returns the results of the match in the form of an array The split( ) method behaves as follows:
  • this method returns an array of strings that represent the splitting of the original string at the delimiter specified by the first argument
  • the substring's returned in the array do not contain the delimiter itself
  • the delimiter may be expressed as a string or a regular expression
  • the optional second argument specifies the maximum length of the returned array
  • when the optional second parameter is omitted, the entire string is split and returned
  • if the delimiter is an empty string, all characters of the original string are split and returned
  • if the delimiter is not specified, the returned array contains one element that resembles the original string

Example:
var sob = new String("a-b-c-d-e-f-g-h-i-j"); sob.split("-",6);// returns a,b,c,d,e,f
Method: substr()

Syntax:
sob.substr(start, length)

Arguments: start, length
start: the starting index of the substring
length: the number of characters for the substring
substr() returns a new substring The substr( ) method behaves as follows:
  • substr() returns a substring derived from the original string
  • the returned substring starts at the position specified by the start argument
  • the returned substring includes all subsequent characters as governed by the length argument
  • the substr() method has been deprecated; it is not specified in ECMAScript
  • when the length argument is omitted, the returned substring contains all characters from the original string
  • the starting position can be a negative number that determines the starting position from the end of the string
Example:
var sob = new String("1st street apt#3"); sob.substr(1,2);// returns st
Method: substring()

Syntax:
sob.substring(start, end)

Arguments: start, end
start: the starting index of the substring
end: the ending index of the substring
substring() returns a new substring The substring( ) method behaves as follows:
  • substring() returns a substring derived from the original string
  • the returned substring starts at the position specified by the start argument
  • the returned substring ends at (but not including) the position specified by the end argument
  • when the end argument is omitted, the returned substring contains all characters from the original string
  • when start is identical to the end argument, the returned string is empty
Example:
var sob = new String("1st street apt#3"); sob.substring(1,2);// returns s
Method: toLowerCase()

Syntax:
sob.toLowerCase( )

Argument: none
returns a copy of string The toLowerCase( ) method behaves as follows:
  • returns a copy of the String object with all uppercase letters converted to lowercase
  • the method toLocaleLowerCase() perform identical to toLowerCase() except for a very few languages with locale mappings

Example:
var sob = new String("The sob string"); sob.toLowerCase(); //returns "the sob string"
Method: toString()

Syntax:
sob.toString(string)

Argument: string
returns the string The toString( ) method behaves as follows:
  • returns the primitive string value of the String object
  • the method will throw a TypeError exception when the method is invoked on an object that is not a string
  • the toString method and the valueOf method will return the same primitive string value

Example:
var sob = new String("The sob string"); sob.toString();// returns string
Method: toUpperCase()

Syntax:
sob.toUpperCase( )

Argument: none
returns a copy of string The toUpperCase( ) method behaves as follows:
  • returns a copy of the String object with all lowercase letters converted to uppercase
  • the method toLocaleUpperCase() perform identical to toUpperCase() except for a very few languages with locale mappings

Example:
var sob = new String("The sob string"); sob.toUpperCase(); //returns "THE SOB STRING"
Method: valueOf()

Syntax:
sob.valueOf( )

Argument: none
returns a primitive string value The valueOf( ) method behaves as follows:
  • valueOf( ) returns the primitive string value contained in the String object
  • the method will throw a TypeError exception when the method is invoked on an object that is not a String.
Example:
var sob = new String("The sob string"); sob.valueOf(); //returns "The sob string"




String Class Constructor


String Class Constructor Example

var sob = new String("A String Object"); sob.valueOf();//returns the primitive string value // returns "A String Object"

Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson