A string is a combination of letters, numbers, special characters, and arithmetic values or a combination of all. JavaScript string can be created by enclosing the string character(literal) either in single quotes (‘) or in double-quotes (“), as shown in the following example:
Example:
var myString = 'Learn JavaScript!'; // Single quoted string
var myString = "Learn JavaScript!"; // Double quoted string
You can use single quotes inside single-quoted strings or double quotes inside double-quoted JavaScript strings by escaping the quotes with a backslash character (\), like this:
The backslash (\) is called an escape character and the sequences \’ and \” that we’ve used in the above example are called escape sequences.
Escape Sequences of JavaScript:
Escape sequences are useful in situations when you want to use characters that can’t be typed using a keyboard. The following are some most commonly used escape sequences.
\n is replaced by the newline character
\t is replaced by the tab character
\r is replaced by the carriage-return character
\b is replaced by the backspace character
\\ is replaced by a single backslash (\)
Performing Operations on Strings:
JavaScript provides some properties and methods to perform operations on string values. Following are some string properties and methods:
Finding the lenght of a string:
The JavaScript length property returns the length of the string, which is the number of characters contained in the string. It includes the number of special characters as well, such as \t or \n.
Example:
var str = "JavaScript is a client-side scripting language and it is used to enhance the interaction of the user with the webpage.";
Note: The length is a property, not a function, so don’t use parentheses after it like str.length(). Instead of this just write str.length, otherwise, it will produce an error.
Finding a String Inside Another String:
The indexOf() method is used to find a substring i.e string within another string. The indexOf() method returns the index or the first occurrence of a specified string inside the string.
Similarly, you can use the lastIndexOf() method to get the index or position of the last occurrence of the specified string within a string, like this:
The search() method is used to search a particular piece of text or a pattern inside a string.
It returns the index of the first match like indexOf() method and returns -1 if no matches were found, but unlike indexOf() method this method can also take a regular expression as an argument to provide advanced search capabilities.
The slice() method is used to extract a part or substring from a string.
The slice() method takes 2 parameters: start index (index at which to begin extraction), and an optional end index (index before which to end extraction), like str.slice(startIndex, endIndex).
The following example slices out a portion of a string from position 10 to position 21:
You can also specify negative values to extract the substring from the string.
If the negative value is specified then the position is counted from the end of the string.
Extracting a Substring from a String using substring() Method:
The substring() method is used to extract a part or substring from a string and it is similar to the slice() method.
The only difference is that the substring() method cannot accept negative indexes.
Extracting a Fixed Number of Characters from a String:
The substr() method provided by JavaScript is similar to the slice() method with a subtle difference, the second parameter specifies the number of characters to extract instead of ending index, like str.substr(startIndex, length).
The replace() method is used to replace part of a string with another string. It takes two parameters a regular expression to match a substring to be replaced and a replacement string, like str.replace(regexp|substr, newSubstr).
It return a new string, it doesn’t affect the original string that will remain unchanged.
Example:
var str = document.getElementById("trial").innerHTML;
var txt = str.replace("client-side scripting language","used to enhance the interaction of the user with the webpage");
Note: JavaScript also provides concat() method to combine two strings, but it is not recommended.
Accessing Individual Characters from a String:
The charAt() method is used to access individual characters from a string, like str.charAt(index). The index specified should be an integer between 0 and str.length – 1. If no index is provided the first character in the string is returned, since the default is 0.
The split() method is used to split a string into an array of strings. The syntax of split() method is str.split(separator, limit). The separator argument specifies the string at which each split should occur and the limit arguments specify the maximum length of the array.
If the separator argument is not found or omitted in the specified string then the entire string is assigned to the first element of the array.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok