JavaScript String Functions
To begin with, a string is an object in Javascript-capable of storing data which is in the text format. In easy words, a character array or a sequence of letter or characters in Javascript can be referred to as strings. A single character in an array has no separate type in Javascript.
A string in Javascript can be created using double quotes or single quotes. You can create a string by a string literal or by the use of a new keyword(string object). Both methods have been explained below:
In case of a string literal, the use of double quotes can help you create a string, given the syntax-
var typeStringNameHere = "You created a string literal";
In case of a string object, the use of new keyword can help you create a string, given the following syntax-
var typeStringNameHere = new String("You created a string object");
Examples of String Functions in JavaScript
Below are the examples:
1. IndexOf()
It will search and will return the index of the first occurrence of a mentioned character or substring within the string. If mentioned character or substring is not found, it will return -1.
var st = "Please only find where 'only' occurs!";
var po = st.indexOf("only");
indexOf() method will return the position of the first occurrence of the mentioned text, that is, 7.
2. lastIndexOf()
This JavaScript String function will search and return the index of the last occurrence of a mentioned character or substring within the string. If mentioned character or substring is not found, it will return -1
var st = "Please only find where 'only' occurs!";
var po = st.lastindexOf("only");
lastIndexOf() method will return the position of the last occurrence of mentioned text, that is, 23
3. search()
It will search and test for a match in a string and returns the index of the match. If mentioned character or substring is not found, it will return -1.
var st = "Please only find where 'only' occurs!";
var po = st.search("only");
search() method will return the position of the first occurrence of the mentioned text, that is, 7.
The difference between the search() method and indexOf() method is that the search() method cannot take the second argument and indexOf() method cannot take regular expressions.
4. slice()
This string function in JavaScript is used to trims a part of a string and returns the trimmed part in a newly created string.
var string = "Mango, Apple, Kiwi";
var r = string.slice(7, 12);
The result of r will be: Apple
var s = "Apple, Kiwi";
var r = s.slice(-10, -6);
The result of r will be: Kiwi
5. substring()
It is the same as the slice() method. The only difference is that substring() does not accept negative indexes.
var s = "Apple, Banana, Kiwi";
var r = s.substring(7, 13);
The result of r is going to be: Banana
6. substr()
It is the same as the slice() method. The only difference is that in substr() the second parameter denotes the length of the first, that is extracted parameter
var s = "Apple, Kiwi";
var r = s.substr(7, 4);
The result of r will be: Kiwi
7. replace(x,y)
This method replaces the first parameter(x) with the second parameter(y) in the string:
var s = "Please visit Oracle!";
var n = s.replace("Oracle", "Microsoft");
The result of n will be: Please visit Microsoft!
8. charAt(y)
Returns the character that is located at the “y” position in the string.
var s = "WORLD";
var r = s.charAt(3);
The result of r will be: L
9. charCodeAt(y)
This method will return the Unicode value of the character that is located at position “y” in the string.
var str = "Halloween";
var r = str.charCodeAt(0);
The result of r will be: 72
10. toLowerCase()
This JavaScript string function will Return the string with all the characters converted to lowercase.
var m = 'PYTHON';
var r = m.toLowerCase();
The result of r will be: python
11. toUpperCase()
This JavaScript string function will Return the string with all the characters converted to uppercase.
var m = "python";
var r = m.toUpperCase();
The result of r will be: PYTHON
12. concat(v1, v2,…)
This method will combine one or more than one string into the original one and return the concatenated string. Original string won’t be modified.
var t1 = "Hi";
var t2 = "What’s up!";
var t3 = t1.concat(" ", t2);
The result of t3 will be: Hi What’s up!
13. trim()
This method will remove any whitespaces from both the start and end of a string:
var s = " Hi What’s up! ";
var b = s.trim());
The result of b will be: “Hi What’s up!”
14. split(delimiter)
This method will split a string into array items as per the specified parameter (delimiter) and returns an array consisting of each element.
var message="Welcome to hell !"
var word=message.split("t");
word[0] contains “Welcome” and word[1] contains “ to hell !”
15. endsWith()
This method finds out if the string is ending with the characters of a mentioned string. This method returns true if the string is ending with the characters provided, and false if not.
var s = "Hello guys";
var n = s.endsWith("guys");
The result of n will be: TRUE
16. startsWith()
This method finds out if the string is starting with the characters of a mentioned string. This method returns true if the string is starting with the characters provided, and false if not.
var s = "Hello guys";
var n = s.startsWith("Hello");
The result of n will be: TRUE
17. toString()
This method will return the String object value.
var string = "Hello guys!";
var r = string.toString();
The result of n will be: Hello guys!
18. length
This will returns the number of characters that is the length of a string.
var string = "Hello People!";
var n = string.length;
The result of n will be: 12
Conclusion
Thus, we can conclude that javascript strings are really important for web functionality. It’s a really exciting language as well as easy language to dive into. Also, JavaScript is really flexible and versatile. Here we’ve only scratched the surface of all the string functions JavaScript can do and it is constantly evolving.