JS String Object
CSGNetwork FREE Source Code, Javascript Source, and Code Snippets!
CSGNetwork Custom Search
     Bookmark and Share
 
CSGNetwork
Snippets
Calculators
Top Free Apps
Home
Back



Right click this window and select "view source" in order to copy the source for this script.

String Object

Last updated: July 6th, 2004

The String object of JavaScript allows you to perform manipulations on a stored piece of text, such as extracting a substring, searching for the occurrence of a certain character within it etc. For example:

var sitename="JavaScript Kit" //example string
alert(sitename.length) //alerts 14, the total number of characters

Properties

Properties Description
length Returns the length of the string (# of characters).
prototype Use this property to attach additional properties and/or methods that get reflected in all instances of the String.

Methods

Note: "[]" surrounding a parameter below means the parameter is optional.

Methods Description
anchor(name) Returns the string with the tag <A name="name"> surrounding it.
big() Returns the string with the tag <BIG> surrounding it.
blink() Returns the string with the tag <BLINK> surrounding it.
bold() Returns the string with the tag <B> surrounding it.
fixed() Returns the string with the tag <TT> surrounding it.
fontcolor(color) Returns the string with the tag <FONT color="color"> surrounding it. Example(s).
fontsize(size) Returns the string with the tag <FONT size="size"> surrounding it.
italics() Returns the string with the tag <I> surrounding it.
link(url) Returns the string with the tag <A href="url"> surrounding it.
small() Returns the string with the tag <SMALL> surrounding it.
strike() Returns the string with the tag <STRIKE> surrounding it.
sub() Returns the string with the tag <SUB> surrounding it.
sup() Returns the string with the tag <SUP> surrounding it.
charAt(x) Returns the character at the "x" position within the string.
charCodeAt(x) Returns the Unicode value of the character at position "x" within the string.
concat(v1, v2,...) Combines one or more strings (arguments v1, v2 etc) into the existing one and returns the combined string. Original string is not modified. Example(s)
fromCharCode(c1, c2,...) Returns a string created by using the specified sequence of Unicode values (arguments c1, c2 etc). Method of String object, not String instance. For example: String.fromCharCode().
indexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. "Start" is an optional argument specifying the position within string to begin the search. Default is 0. Example(s)
lastIndexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. Searches the string from end to beginning. If not found, -1 is returned. "Start" is an optional argument specifying the position within string to begin the search. Default is string.length-1.
match(regexp) Executes a search for a match within a string based on a regular expression. It returns an array of information or null if no match is found.
replace( regexp, replacetext) Searches and replaces the regular expression portion (match) with the replaced text instead.
search(regexp) Tests for a match in a string. It returns the index of the match, or -1 if not found.
slice(start, [end]) Returns a substring of the string based on the "start" and "end" index arguments, NOT including the "end" index itself. "End" is optional, and if none is specified, the slice includes all characters from "start" to end of string.
split(delimiter, [limit]) Splits a string into many according to the specified delimiter, and returns an array containing each element. The optional "limit" is an integer that lets you specify the maximum number of elements to return. Example(s)
substr(start, [length]) Returns the characters in a string beginning at "start" and through the specified number of characters, "length". "Length" is optional, and if omitted, up to the end of the string is assumed. Example(s)
substring(from, [to]) Returns the characters in a string between "from" and "to" indexes, NOT including "to" inself. "To" is optional, and if omitted, up to the end of the string is assumed. Example(s)
toLowerCase() Returns the string with all of its characters converted to lowercase.
toUpperCase() Returns the string with all of its characters converted to uppercase.

Examples

fontcolor(color)

var myname="Peter Don"
document.write(myname.fontcolor("blue")) //writes out "Peter Don" in blue

concat(v1, v2,...)

var he="Bob"
var she="Jane"
var final=he.concat(" loves ", she) // "Bob loves Jane"

indexOf(substr, [start])

var message="abcdefg"
message.indexOf("h") //returns -1, as "h" isn't in string

var myname="John Miller"
myname.indexOf("Miller") //returns 5, the starting index of "Miller"

split(delimiter, [limit])

var sitename="Welcome to JavaScript Kit"
var words=sitename.split(" ") //split using blank space as delimiter
for (i=0; i<words.length; i++)
alert(words[i]) //alerts "Welcome", "to", "JavaScript", and "Kit"

substr(start, [length])

var sitename="Welcome to JavaScript Kit"
alert(sitename.substr(3, 3)) //alerts "com"

substring(from, [to])

var sitename="Welcome to JavaScript Kit"
alert(sitename.substring(0, 2)) //alerts "We"



Bookmark and Share
Registered® Trademark™ and Copyright© 1973 - CSG, Computer Support Group, Inc. and CSGNetwork.Com All Rights Reserved