JavaScript/Strings

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Numbers Index Next: Dates

A string is a type of variable which stores a string (chain of characters).

Basic Use[edit]

To make a new string, you can make a variable and give it a value of new String().

var foo = new String();

But, most developers skip that part and use a string literal:

var foo = "my string";

After you have made your string, you can edit it as you like:

foo = "bar"; //foo = "bar"
foo = "barblah"; //foo = "barblah"
foo += "bar"; //foo = "barblahbar"

A string literal is normally delimited by the '  or "  character, and can normally contain almost any character. Common convention differs on whether to use single quotes or double quotes for strings. Some developers are for single quotes (Crockford, Amaram, Sakalos, Michaux) while others are for double quotes (NextApp, Murray, Dojo). Whichever method you choose, try to be consistent in how you apply it.

Due to the delimiters, it's not possible to directly place either the single or double quote within the string when it's used to start or end the string. In order to work around that limitation, you can either switch to the other type of delimiter for that case, or place a backslash before the quote to ensure that it appears within the string:

foo = 'The cat says, "Meow!"';
foo = "The cat says, \"Meow!\"";
foo = "It's \"cold\" today.";
foo = 'It\'s "cold" today.';

Properties and methods of the String() object[edit]

As with all objects, Strings have some methods and properties.

replace(text, newtext)[edit]

The replace() function returns a string with content replaced. Only the first occurrence is replaced.

var foo = "foo bar foo bar foo";
var newString = foo.replace("bar", "NEW!")
alert(foo); //foo bar foo bar foo
alert(newString); //foo NEW! foo bar foo

As you can see, the replace() function only returns the new content and does not modify the 'foo' object.

concat(text)[edit]

The concat() function joins two strings.

var foo = "Hello";
var bar = foo.concat(" World!")
alert(bar); //Hello World!

toUpperCase()[edit]

This function returns the current string in upper case.

var foo = "Hello!";
alert(foo.toUpperCase()); // HELLO!

toLowerCase()[edit]

This function returns the current string in lower case.

var foo = "Hello!";
alert(foo.toLowerCase()); // hello!

length[edit]

Returns the length as an integer.

var foo = "Hello!";
alert(foo.length); // 6

substring(start[, end])[edit]

Substring extracts characters from the start position

"hello".substring(1); // "ello"

When the end is provided, they are extracted up to but not including the end position.

"hello".substring(1, 3); // "el"

Substring always works from left to right. If the start position is larger than the end position, Substring will swap the values; although sometimes useful, this is not always what you want; different behavior is provided by slice.

"hello".substring(3, 1); // "el"

slice(start[, end])[edit]

Slice extracts characters from the start position, essentially the same as substring

"hello".slice(1); // "ello"

When the end is provided, they are extracted up to but not including the end position.

"hello".slice(1, 3); // "el"

Slice allows you to extract text referenced from the end of the string by using negative indexing.

"hello".slice(-4, -2); // "el"

Unlike Substring, the Slice method never swaps the start and end positions. If the start is after the end, Slice will attempt to extract the content as presented, but will most likely provide unexpected results.

"hello".slice(3, 1); // ""

substr(start[, number of characters])[edit]

substr extracts characters from the start position, essentially the same as substring/slice

"hello".substr(1); // "ello"

When the number of characters is provided, they are extracted by count.

"hello".substr(1, 3); // "ell"

Further reading[edit]


Previous: Numbers Index Next: Dates