String Function (string)

Course- Javascript >

A string is a sequence of characters from within a given character set (for example, the ASCII or Unicode character sets) and is usually used to store text.

You define a string by enclosing it in single or double quotes:

var myString = "This is a string";

You can define an empty string by using two quote marks with nothing between them:

var myString = "";

Escape Sequences

Some characters that you want to put in a string may not have associated keys on the keyboard, or may be special characters that for other reasons can’t occur in a string. Examples include the tab character, the new line character, and the single or double quotes that enclose the string itself. To use such a character in a string, it must be represented by the character preceded by a backslash ( \ ), a combination that JavaScript interprets as the desired special character. Such a combination is known as an escape sequence.

Suppose that you wanted to enter some “new line” characters into a string, so that when the string is shown by a call to the alert() method, it will be split into several lines:

var message = "IMPORTANT MESSAGE:\n\nError detected!\nPlease check your
data";
alert(message);                    
                    

The result of inserting these escape sequences is shown

escape sequences

Some common escape sequences

Some common escape sequences

String Methods

Some common string methods!

Some common string methods

concat

You’ve already had experience in earlier sections of joining strings together using the + operator. This is known as string concatenation, and JavaScript strings have a concat() method offering additional capabilities:

var string1 = "The quick brown fox ";
var string2 = "jumps over the lazy dog";
var longString = string1.concat(string2);
                    

indexOf()

We can use indexOf() to find the first place where a particular substring of one or more characters occurs in a string. The method returns the index (the position) of the searched-for substring, or -1 if it isn’t found anywhere in the string:

var string1 = "The quick brown fox ";
string1.indexOf('fox') // returns 16
string1.indexOf('dog') // returns -1                    
                    

Remember that the index of the first character in a string is 0, not 1.

lastIndexOf()

As you’ll have guessed, lastIndexOf() works just the same way as indexOf(), but finds the last occurrence of the substring, rather than the first.

replace()

Searches for a match between a substring and a string, and returns a new string with the substring replaced by a new substring:

var string1 = "The quick brown fox ";
var string2 = string1.replace("brown", "orange"); // string2 is now
"the quick
orange fox"                    
                    

split()

Used to split a string into an array of substrings and return the new array:

here to view code image
var string1 = "The quick brown fox ";
var newArray = string1.split(" ")                    
                    

substr()

The substr() method takes one or two arguments. The first is the starting index—substr() extracts the characters from a string, beginning at the starting index, for the specified number of characters, returning the new substring. The second parameter (number of characters) is optional, and if omitted, all of the remaining string will be extracted:

var string1 = "The quick brown fox ";
var sub1 = string1.substr(4, 11); // extracts "quick brown"
var sub2 = string1.substr(4); // extracts "quick brown fox"                    
                    

toLowerCase() and toUpperCase()

Puts the string into all uppercase or all lowercase:

var string1 = "The quick brown fox ";
var sub1 = string1.toLowerCase(); // sub1 contains "the quick brown fox
" var sub2 =
string1.toUpperCase(); // sub2 contains "
THE QUICK BROWN FOX
"