String.length()
This method returns the number of characters and spaces in a string.
_4let string1 = "JavaScript j";_4let len = string1.length;_4_4console.log(len); // 12
String.replace()
The
replace()
method returns a new stringIt replaces with the specified
string
orregex
_8const p = "The quick brown fox jumps over the lazy dog.";_8_8console.log(p.replace("dog", "monkey"));_8// "The quick brown fox jumps over the lazy monkey."_8_8const regex = /Dog/i;_8console.log(p.replace(regex, "ferret"));_8// "The quick brown fox jumps over the lazy ferret."
String.indexOf()
Returns the index of value first occurrence of substring.
_13var str = "JavaScript is the world's most misunderstood programming language.";_13_13var index1 = str.indexOf("language");_13console.log(index1); // 57_13_13var index2 = str.indexOf("p");_13console.log(index2); // 8_13_13var index3 = str.indexOf("p", 9);_13console.log(index3); // 45_13_13var index4 = str.indexOf("Python");_13console.log(index4); // -1
String.lastIndexOf()
Returns the index of the last occurrence of the specified substring.
_5var str = "JavaScript is the world's most misunderstood programming language.";_5var substr = "m";_5_5var result = str.lastIndexOf(substr);_5console.log(result); // 52
String.startsWith()
Returns true
if a string begins with specified character(s). If not, it returns false
.
_8const message =_8 "JavaScript is the world's most misunderstood programming language";_8_8let result = message.startsWith("JavaScript");_8console.log(result); // true_8_8result = message.startsWith("Script");_8console.log(result); // false
String.endsWith()
Returns true
if a string ends with the specified string. If not, the method returns false
.
_8let sentence =_8 "JavaScript is the world's most misunderstood programming language";_8_8let check = sentence.endsWith("language");_8console.log(check); // true_8_8let check1 = sentence.endsWith("is");_8console.log(check1); // false
String.toUpperCase()
The method returns a string
in uppercase form.
_6let message =_6 "JavaScript is the world's most misunderstood programming language";_6_6const upperMessage = message.toUpperCase();_6console.log(upperMessage);_6// JAVASCRIPT IS THE WORLD'S MOST MISUNDERSTOOD PROGRAMMING LANGUAGE
String.toLowerCase()
The method returns a string
in lowercase form.
_6let message =_6 "JAVASCRIPT IS THE WORLD'S MOST MISUNDERSTOOD PROGRAMMING LANGUAGE";_6_6const lowerMessage = message.toLowerCase();_6console.log(lowerMessage);_6// javascript is the world's most misunderstood programming language
String.includes()
- The method checks if one
string
can be found inside anotherstring
- If it presents it will return
true
elsefalse
- It performs a case-sensitive search to determine inside or not
_5const message =_5 "JavaScript is the world's most misunderstood programming language";_5_5let result = message.includes("Java");_5console.log(result); // true
String.repeat()
This method returns a specified string
repeated a given number of times.
_4const message = "JavaScript ";_4_4const result = message.repeat(3);_4console.log(result); // JavaScript JavaScript JavaScript
String.charAt()
This method returns the character of the specified index from a string
.
_4const string = "javascript";_4_4let index1 = string.charAt(1);_4console.log("Character at index 1 is " + index1); // Character at index 1 is a
String.charCodeAt()
Returns Unicode of the character at the given index
_5const string = "javascript";_5_5let index1 = string.charCodeAt(1);_5console.log("Unicode of Character at index 1 is " + index1);_5// Unicode of the Character at index 1 is 97
String.fromCharCode()
The method returns a string created from the specified sequence of UTF-16 code units.
_2let string1 = String.fromCharCode(72, 69, 76, 76, 79);_2console.log(string1); // HELLO
String.substring()
The method returns the part of the string between the start and end indexes, or to the end of the string
.
_4const message = "javascript";_4_4console.log(message.substring(1, 3)); // "av"_4console.log(message.substring(2)); // "vascript"
String.match()
Returns the matching string with a regex
pattern.
_5const message = " Welcome to JavaScript!";_5const regex = /[A-Z]/g;_5const result = message.match(regex);_5_5console.log(result); // ["W", "J"]
String.matchAll()
Returns an iterator of all matching a string with a regex
pattern.
_7const regexp = /t(e)(st(\d?))/g;_7const str = "test1test2";_7_7const array = [...str.matchAll(regexp)];_7_7console.log(array[0]); // Array ["test1", "e", "st1", "1"]_7console.log(array[1]); // Array ["test2", "e", "st2", "2"]
String.split()
String divided into the list of substring
_7const string = "The quick brown fox jumps over the lazy dog.";_7_7console.log(string.split());_7// ["The quick brown fox jumps over the lazy dog."]_7_7console.log(string.split(" "));_7// ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
String.slice()
The method extracts a section of a string and returns it as a new string, without modifying the original string.
_13const str = "The quick brown fox jumps over the lazy dog.";_13_13console.log(str.slice(31));_13// "the lazy dog."_13_13console.log(str.slice(4, 19));_13// "quick brown fox"_13_13console.log(str.slice(-4));_13// "dog."_13_13console.log(str.slice(-9, -5));_13// "lazy"
String.trim()
The method removes whitespace from both ends of a string
and returns a new string
, without modifying the original string
.
_3const greeting = " Hello world! ";_3console.log(greeting.trim());_3// "Hello world!";
String.trimEnd()
The method removes whitespace from the end of a string
.
_3const greeting = " Hello world! ";_3console.log(greeting.trimEnd());_3// " Hello world!";
String.trimStart()
The method removes whitespace from the beginning of a string
.
_3const greeting = " Hello world! ";_3console.log(greeting.trimStart());_3// "Hello world! ";