JavaScript Tutorial/String/Introduction
Special Characters (escape sequence)
A backslash character (\) creates special characters.
Escape Sequence Character \b Backspace \f Form feed \n Newline \r Carriage return \t Tab \" Single quote \" Double quote \\ Backslash \XXX Character represented by three octal digits XXX (0 to 377) \XX Character represented by two hexadecimal digits XX (00 to FF) \uXXXX Unicode character represented by four hexadecimal digits.
String() Object
Strings are declared by placing the characters between a pair of double quotes (" ") or single quotes ("").
JavaScript interprets single quotes as part of the string if the single quotes are inside a pair of double quotes.
JavaScript interprets double quotes as part of the string if the double quotes are inside a pair of single quotes.
Or, you will you need to use escape sequences.
Syntax to create String object
var variable = new String(string);
"string"
The String() object is one of the core JavaScript objects.
Instances are created when a program constructs an instance using the new keyword and passing it the String() object.
Properties and Methods Used by the String Object
Method/Property
Description
anchor()
Creates an instance of the <A> tag with the NAME attribute set to the string passed to the method.
big()
Converts the string into an instance of the tag.
blink()
Converts the string into an instance of the <BLINK> tag.
bold()
Converts the string into an instance of the <BOLD> tag.
charAt()
Returns the character at the index passed to the method.
charCodeAt()
Returns the ISO-Latin-1 number of the character at the index passed to the method.
concat()
Concatenates the two strings passed to return a new string. This method was added in JavaScript 1.2.
fixed()
Converts the string into an instance of the , fixed pitch font tag.
fontcolor()
Sets the COLOR attribute of an instance of the tag.
fontsize()
Sets the SIZE attribute of an instance of the tag.
fromCharCode()
Returns the string value of the ISO-Latin-1 number passed to the method.
indexOf()
Returns the index of the first occurrence of the string passed to the method within an instance of a String object.
italics()
Converts the string into an instance of the tag.
lastIndexOf()
Returns the index of the last occurrence of the string passed to the method within an instance of a String object.
link()
Converts the string into an instance of the <A> tag and sets the HREF attribute with the URL that is passed to the method.
match()
Returns an array containing the matches found based on the regular expression passed to the method.
replace()
Performs a search and replace, using the regular expression and replace string passed to the method, on the instance of a String that calls it.
search()
Returns the index location of the match found in the string passed to the method. A -1 is returned if the string is not found.
slice()
Returns the string between the beginning and ending index passed to the method. If a negative number is passed, the index is referenced from the end of the string passed.
small()
Converts the string into an instance of the tag.
split()
Returns the string split into segments defined by the string and instance limit passed to the method.
strike()
Converts the string into an instance of the tag.
sub()
Converts the string into an instance of the tag.
substr()
Returns the string beginning with the indexed location and number of characters to return. If a negative number is passed, the index is referenced from the end of the string passed.
substring()
Returns the string between the beginning and ending index passed to the method.
sup()
Converts the string into an instance of the tag.
toLowerCase()
Converts all the characters in the string to lowercase.
toSource()
Returns the string representation of the String passed.
toString()
Returns the characters passed as type string.
toUpperCase()
Converts all the characters in the string to uppercase.
length
Returns the length of the string.
prototype
Provides the ability for a programmer to add properties to instances of the String object.
The String Type
The String type is the only primitive type that doesn"t have a definite size.
A string can be used to store zero or more Unicode characters.
Each character in a string is given a position.
The position starts with the first character in position 0, the second character in position 1.
The position of the final character in a string is always the length of the string minus 1.
String literals are specified by using either double quotes (") or single quotes (").
JavaScript has no character type.
var sColor1 = "blue";
var sColor2 = "blue";
The following table lists the JavaScript character literals:
Literal Meaning \n Newline \t Tab \b Backspace \r Carriage return \f Formfeed \\ Backslash \" Single quote \" Double quote \0nnn A character represented by octal code nnn (where n is an octal digit 0-7) \xnn A character represented by hexadecimal code nn (where n is a hexadecimal digit 0-F) \unnnn A Unicode character represented by hexadecimal code nnnn (where n is a hexadecimal digit 0-F)