Software Sue

Link to Simply Sue
Link to Sustainability Sue
location: software/scripting/javascript/string operators Skip navigation : Home  

JavaScript String Operators


Strings became available in core Javascript 1.0, and were enhanced in Navigator 3.

new String(value) has been used to construct a new string since Javascript 1.1.

The only property that strings have is length (since Javascript 1.0, ECMA-262).

String Operators

Comparison operators can be used on string values.

The concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.

"horse " + "and " + "carriage" returns the string "horse and carriage".

There is also a shorthand form:

samplestring = "dog"
samplestring += "sled"

results in samplestring containing "dogsled".

Tinkering with strings

You can play around with strings, changing their colour, makeing them bold, or italicised, or uppercase, or lowercase, or a variety of other things. You can also take a string that has been made bold, for example, then make it italic, or change it's colour, etc.

Here's the body of the javascript from the <head>:

var first = "This is the first string, intended to be bold."
var second = "This is the second string, intended to be italicised."
var third = "This is the third string, intended to be uppercase."
var fourth = "This is the fourth string, intended to be green. Then a copy is made italic too. Then a copy of that is made uppercase."

// change the look of the strings
var first_bold = first.bold();
var second_italic = second.italics();
var third_upper = third.toUpperCase();
var fourth_green = fourth.fontcolor('green');
var fifth_green_italic = fourth_green.italics();
var sixth_green_italic_upper = fifth_green_italic.toUpperCase();

The strings are then simply written out using document.writeln in the body.

String Methods

anchor(name)

Adds an HTML anchor to a string.

big()

Makes a string <BIG>.

blink()

Makes a string <BLINK>.

bold()

Makes a string bold with <B>.

charAt(n)

Gets the nth character from a string. Javascript 1.0; ECMA-262.

charCodeAt(n)

Gets the nth character code from a string. Javascript 1.2; ECMA-262.

concat(value, ...)

Concatenates strings. Javascript 1.2.

fixed()

Makes a string fixed-width<TT>.

fontcolor(colour)

Sets a string's colour with <FONT>.

fontsize(size)

Sets a string's font size with <FONT>.

indexOf(substring, start)

Searches a string. Javascript 1.0; ECMA-262.

italics()

Makes a string italic with <I>.

lastIndexOf(substring, start)

Searches a string backwards. Javascript 1.0; ECMA-262.

link(href)

Adds a hypertext link to a string.

match(regexp)

Finds one or more regular expression matches. Javascript 1.2.

replace(regexp, replacement)

Replaces substring(s) matching a regular expression. Javascript 1.2.

search(regexp)

Searches for a regular expression. Javascript 1.2.

slice(start, end)

Extracts a substring. Javascript 1.2.

small()

Makes a string <SMALL>.

split(delimiter)

Breaks a string into an array of strings. Javascript 1.1; ECMA-262.

strike()

Strikes out a string with <STRIKE>.

sub()

Makes a string a subscript with <SUB>.

substring(from, to)

Returns a substring of a string. Javascript 1.0; ECMA-262.

substr(start, length)

Extract a substring. Javascript 1.2.

sup()

Makes a string a superscript with <SUP>.

toLowerCase()

Converts a string to lowercase. Javascript 1.0; ECMA-262.

toUpperCase()

Converts a string to uppercase. Javascript 1.0; ECMA-262.

Static Methods

String.fromChar(c1, c2, ...)

Creates a string from character encodings. Javascript 1.2; ECMA-262.