JavaScript Objects :
STRING OBJECT








The string object makes it easier to handle strings.

The following methods and properties are available for strings:

Note:
In the examples, we assume that a variable called MyString
has been assigned the value "Go Johnny, Go Go Go".


OBJECTEXPLANATION
lengthReturns the length of the string
MyString.length would return 19
charAt(n)Returns the character at the specified
position in the string.
 
MyString.charAt(3)
would return the character "J".
 
Note:
J is the fourth character in the string,
but since counting starts with 0 the fourth
character is referred to as number 3.
charCodeAt(n)
Returns the character code at the
specified position in the string.
 
MyString.charCodeAt(4)
would return the value 74.
 
The reason is that J is character #74 in
the so called ISO-Latin-1 codeset.
indexOf(string[,n])Returns the first position of the substring.
 
MyString.indexOf("Go")
would return 0.
 
Note:
If a substring is at the beginning of a
string the position is zero.
If a substring is not present within a
string the value is -1.
 
MyString.indexOf("Go",3)
would return 11.
 
Note:
The search for "Go" starts at the third
character, but the counting still starts
at the first.
The first presence of Go (if presence
before the third character is omitted) is
at the 12th character. Since the first
character is referred to as number zero
the result is 11.
lastIndexOf(substring[,n])Returns the last position of the substring.
 
MyString.lastIndexOf("Go")
would return 17.
 
Note:
If a substring is at the beginning of a
string the position is zero.
The last position in a string is therefore
one less than the length of the string.
If a substring is not present within a
string the value is -1.
fromCharCode(x[,y,z])Constructs a string from the specified
sequence of ISO-Latin-1 codeset values
 
String.fromCharCode(74,75,76)
would return "JKL".
substring(x,y)Returns the specified subset of the string,
by specifying the start and end indexes
 
MyString.substring(3,9)
would return "Johnny".
 
Note:
The first character is number zero.
x indicates the start of the substring.
y indicates the end of the substring.
Thus, y is NOT the number of characters
to include, starting at character x.
toLowerCase()Returns the string in all lowercase
 
MyString.toLowerCase()
would return "go johnny, go, go, go".
toUpperCase()Returns the string in all uppercase
 
MyString.toUpperCase()
would return "GO JOHNNY, GO, GO, GO".
split(separator[,n])Splits a string into an array of strings
by separating the string into substrings
 
MyArrayOfS=MyString.split(", ")
would result in the creation of an array:
 
MyArrayOfStrings[0]
would be "Go Johnny"
 
MyArrayOfStrings[1]
would be "Go"
 
MyArrayOfStrings[2]
would be "Go"
 
MyArrayOfStrings[3]
would be "Go"
 
The number of elements in the array
is stored in MySplitString.length.



MyArrayOfStrings=MyString.split(", ",2)
would result in a different array:
 
MyArrayOfStrings[0]
would be "Go Johnny"
 
MyArrayOfStrings[1]
would be "Go"
 
By entering a limit on the number
of splits, we reduced the array to 2 entries.
 
NOTE: Javascript 1.2 or higher!
slice(x,y)Does exactly the same as substring.
 
NOTE: Javascript 1.3 or higher!
substr(x,y)Returns a subset of a string.
 
This is almost the same as substring.
The difference is that, in substring
you enter startindex and endindex,
while in substr you enter startindex
and numbers of characters to include.
 
substr(3,6)
would return "Johnny",
which is the same as substring(3,9)
 
NOTE: Javascript 1.3 or higher!
matchCompare variables.
 
Use this syntax instead:
if(varibel1==variabel2) {bla bla bla}
 
NOTE: Javascript 1.3 or higher!
replace(/subtext/[gi])Replaces substrings of a string.
 
MyString.replace(/Go/, "Up")
would return "Up Johnny, Go, Go, Go".
 
MyString.replace(/Go/g, "Up")
would return "Up Johnny, Up, Up, Up".
/Go/g forces a global replace.
Not just replacing of the first "Go".
 
MyString.replace(/go/gi, "Up")
would return "Up Johnny, Up, Up, Up".
/go/i forces the replace to ignore
upper and lower case differences.
 
NOTE: Javascript 1.3 or higher!
searchSearches for substrings in a string.
 
Use substring instead.
 
NOTE: Javascript 1.3 or higher!

Click here for the object definition provided by DevEdge.















DEVELOPER TIP!





     "Better Than Books - As Easy As It Gets!"