A string in Javascript represents a sequence of characters.
Strings are useful for holding data that can be represented in textual forms. Some of the most-used operations on strings are to check their length, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.
Whatever manipulation you need to do with Strings here are 12 Javascript String methods you can always reference.
The search() method matches a string against a regular expression **
It returns the index (position) of the first match.
The search() method returns -1 if no match is found.
The search() method is also case sensitive.
const str = "The quick brown fox jumps over the lazy dog";
str.search(/[^\w\s]/g); The split() method splits a string into an array of substrings.
The split() method returns the new array.
The split() method does not change the original string.
If (” “) is used as separator, the string is split between words.
const str = "The quick brown fox jumps over the lazy dog";
str.split(','); The repeat() method returns a string with a number of copies of a string.
The repeat() method returns a new string.
The repeat() method does not change the original string.
const str = "The quick brown fox jumps over the lazy dog";
str.repeat(3); The length property returns the length of a string.
The length property of an empty string is 0.
const str = "The quick brown fox jumps over the lazy dog";
str.length; The replace() method searches a string for a value or a regular expression.
The replace() method returns a new string with the value(s) replaced.
The replace() method does not change the original string.
const str = "The quick brown fox jumps over the lazy dog";
str.replace('o', 'a'); The indexOf() method returns the position of the first occurrence of a value in a string.
The indexOf() method returns -1 if the value is not found.
The indexOf() method is case sensitive.
const str = "The quick brown fox jumps over the lazy dog";
str.indexOf('fox'); The charAt() method in Ja returns the character at a specified index (position) in a string.
const str = "The quick brown fox jumps over the lazy dog";
str.indexOf('fox'); The startsWith() method returns true if a string starts with a specified string, otherwise it returns false. The startsWith() method is case sensitive.
const str = "The quick brown fox jumps over the lazy dog";
str.startsWith("The"); The toUpperCase() method converts a string to uppercase letters and does not change the original string.
const str = "The quick brown fox jumps over the lazy dog";
str.toUpperCase(); The toLowerCase() method converts a string to uppercase letters and does not change the original string.
const str = "The quick brown fox jumps over the lazy dog";
str.toLowerCase(); The match() method matches a string against a regular expression **
The match() method returns an array with the matches.
The match() method returns null if no match is found.
const str = "The quick brown fox jumps over the lazy dog";
str.match(/[A-Z]/g); Latest tech news and coding tips.
Context poisoning is a technique where an attacker deliberately places misleading, malicious, or irrelevant information into…
React makes building user interfaces easier, but certain errors appear repeatedly, especially when working with…
The C programming language is a foundational, general-purpose computer programming language created by Dennis Ritchie at Bell Labs in 1972. Originally developed to…
What Is Homebrew? Homebrew is a package manager for macOS and Linux that makes it easy to…
JavaScript gives you several ways to iterate over an array. Some are better for simple…
A web server is the software responsible for receiving requests from clients, processing those requests,…