To find the length of a string, use the built-in length property.
<p id="demo"></p>
<script>
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = text.length;
</script>
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the start position and the end position (not inclusive).
This example slices out a portion of a string from position 7 to position 12 (13-1).
<p id="demo1"></p>
<script>
let str1 = "Apple, Banana, Kiwi";
document.getElementById("demo1").innerHTML = str1.slice(7,13);
</script>
If a parameter is negative, the position is counted from the end of the string.
This example slices out a portion of a string from position -12 to position -6.
<p id="demo2"></p>
<script>
let str2 = "Apple, Banana, Kiwi";
document.getElementById("demo1").innerHTML = str2.slice(-12,-6);
</script>
If you omit the second parameter, the method will slice out the rest of the string.
<p id="demo3"></p>
<script>
let str3 = "Apple, Banana, Kiwi";
document.getElementById("demo3").innerHTML = str3.slice(7);
</script>
or, counting from the end.
<p id="demo4"></p>
<script>
let str4 = "Apple, Banana, Kiwi";
document.getElementById("demo4").innerHTML = str4.slice(-12);
</script>
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
<p id="demo5"></p>
<script>
let str5 = "Apple, Banana, Kiwi";
document.getElementById("demo5").innerHTML = str5.substring(7,13);
</script>
If you omit the second parameter, substr() will slice out the rest of the string.
<p id="demo6"></p>
<script>
let str6 = "Apple, Banana, Kiwi";
document.getElementById("demo6").innerHTML = str6.substring(7);
</script>
substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.
<p id="demo7"></p>
<script>
let str7 = "Apple, Banana, Kiwi";
document.getElementById("demo7").innerHTML = str7.substring(7,6);
</script>
If you omit the second parameter, substr() will slice out the rest of the string.
<p id="demo8"></p>
<script>
let str8 = "Apple, Banana, Kiwi";
document.getElementById("demo8").innerHTML = str8.substring(7);
</script>
If the parameter is negative, substr returns the a string made up of the last N characters, when -N is passed to it.
<p id="demo9"></p>
<script>
let str9 = "Apple, Banana, Kiwi";
document.getElementById("demo9").innerHTML = str9.substring(-4);
</script>
The replace() method replaces a specified value with another value in a string.
Please visit Microsoft!
<button class="btn btn-primary mb-5" onclick="myFunction()">Try it</button>
<p id="demo10">Please visit Microsoft!</p>
<script>
function myFunction() {
let text = document.getElementById("demo10").innerHTML;
document.getElementById("demo10").innerHTML =
text.replace("Microsoft","Apple");
}
</script>
By default, the replace() method is case sensitive. Writing MICROSOFT (with uppercase) will not work.
To replace case insensitive, use a regular expression with an /i flag (insensitive):
Please visit MICROSOFT!
<button class="btn btn-primary mb-5" onclick="myFunction1()">Try it</button>
<p id="demo11">Please visit MICROSOFT!</p>
<script>
function myFunction1() {
let text = document.getElementById("demo11").innerHTML;
document.getElementById("demo11").innerHTML =
text.replace(/Microsoft/i,"Apple");
}
</script>
To replace all matches, use a regular expression with a /g flag (global match).
Please visit Microsoft and Microsoft!
<button class="btn btn-primary mb-5" onclick="myFunction2()">Try it</button>
<p id="demo12">Please visit Microsoft and Microsoft!</p>
<script>
function myFunction2() {
let text = document.getElementById("demo12").innerHTML;
document.getElementById("demo12").innerHTML =
text.replace(/Microsoft/g,"Apple");
}
</script>
The replaceAll() method replaces all instances of a string.
I love cats. Cats are very easy to love. Cats are very popular.
<button class="btn btn-primary mb-5" onclick="myFunction5()">Try it</button>
<p id="demo48">I love cats. Cats are very easy to love. Cats are very popular.</p>
<script>
function myFunction5() {
let text23 = document.getElementById("demo48").innerHTML;
text23 = text23.replaceAll("Cats","Dogs");
text23 = text23.replaceAll("cats","dogs");
document.getElementById("demo48").innerHTML = text23;
}
</script>
The replaceAll() method allows you to specify a regular expression instead of a string to be replaced.
I love cats. Cats are very easy to love. Cats are very popular.
<button class="btn btn-primary mb-5" onclick="myFunction6()">Try it</button>
<p id="demo49">I love cats. Cats are very easy to love. Cats are very popular.</p>
<script>
function myFunction6() {
let text24 = document.getElementById("demo49").innerHTML;
text24 = text24.replaceAll(/Cats/g,"Dogs");
text24 = text24.replaceAll(/cats/g,"dogs");
document.getElementById("demo49").innerHTML = text24;
}
</script>
A string is converted to upper case with toUpperCase().
Hello World!
<button class="btn btn-primary mb-5" onclick="myFunction3()">Try it</button>
<p id="demo13">Hello World!</p>
<script>
function myFunction3() {
let text = document.getElementById("demo13").innerHTML;
document.getElementById("demo13").innerHTML = text.toUpperCase();
}
</script>
A string is converted to lower case with toLowerCase().
Hello World!
<button class="btn btn-primary mb-5" onclick="myFunction4()">Try it</button>
<p id="demo14">Hello World!</p>
<script>
function myFunction4() {
let text = document.getElementById("demo14").innerHTML;
document.getElementById("demo14").innerHTML = text.toLowerCase();
}
</script>
concat() joins two or more strings.
<p id="demo15"></p>
<script>
let text1 = "Hello";
let text2 = "World!";
let text3 = text1.concat(" ",text2);
document.getElementById("demo15").innerHTML = text3;
</script>
The trim() method removes whitespace from both sides of a string.
<p id="demo16"></p>
<script>
let text4 = " Hello World! ";
let text5 = text4.trim();
document.getElementById("demo16").innerHTML =
"Length text4 = " + text4.length + "<br>Length2 text5 = " + text5.length;
</script>
The trimStart() method works like trim() but removes whitespace only from the start of a string.
<p id="demo50"></p>
<script>
let text25 = " Hello World! ";
let text26 = text25.trimStart();
document.getElementById("demo50").innerHTML =
"Length text25 = " + text25.length + "<br>Length2 text26 = " + text26.length;
</script>
The trimEnd() method works like trim() but removes whitespace only from the end of a string.
<p id="demo51"></p>
<script>
let text27 = " Hello World! ";
let text28 = text27.trimEnd();
document.getElementById("demo51").innerHTML =
"Length text27 = " + text27.length + "<br>Length2 text28 = " + text28.length;
</script>
The padStart() method pads a string with another string (multiple times) until it reaches a given length. The padding is applied from the start of the current string.
<p id="demo17"></p>
<script>
let text6 = "5";
document.getElementById("demo17").innerHTML = text6.padStart(4,"x");
</script>
The padStart() method is a string method.
To pad a number, convert the number to a string first.
<p id="demo18"></p>
<script>
let numb = 5;
let text7 = numb.toString();
document.getElementById("demo18").innerHTML = text7.padStart(4,0);
</script>
The padEnd() method pads a string with another string (multiple times) until it reaches a given length. The padding is applied at the end of the current string.
<p id="demo19"></p>
<script>
let text8 = "5";
document.getElementById("demo19").innerHTML = text8.padEnd(4,"x");
</script>
The charAt() method returns the character at a specified index (position) in a string.
<p id="demo20"></p>
<script>
var text9 = "HELLO WORLD";
document.getElementById("demo20").innerHTML = text9.charAt(0);
</script>
The charCodeAt() method returns the unicode of the character at a specified index in a string.
<p id="demo21"></p>
<script>
var text10 = "HELLO WORLD";
document.getElementById("demo21").innerHTML = text10.charCodeAt(0);
</script>
ECMAScript 5 allows property access on strings.
<p id="demo22"></p>
<script>
var str = "HELLO WORLD";
document.getElementById("demo22").innerHTML = str[0];
</script>
A string can be converted to an array with the split() method.
<p id="demo23"></p>
<script>
var text_1 = "a,b,c,d,e,f";
const myArray = text_1.split(",");
text_1 = "";
for (let i = 0; i < myArray.length; i++) {
text_1 += myArray[i] + "<br>"
}
document.getElementById("demo23").innerHTML = text_1;
</script>
If the separator is omitted, the returned array will contain the whole string in index [0].
<p id="demo24"></p>
<script>
var text_2 = "abcdef";
const myArray_1 = text_2.split();
document.getElementById("demo24").innerHTML = myArray_1[0];
</script>
If the separator is "", the returned array will be an array of single characters.
<p id="demo25"></p>
<script>
let text_3 = "Hello";
const myArray_2 = text_3.split("");
text_3 = "";
for (let i = 0; i < myArray_2.length; i++) {
text_3 += myArray_2[i] + "<br>"
}
document.getElementById("demo23").innerHTML = text_3;
</script>
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
<p id="demo26"></p>
<script>
let text_4 = "Please locate where 'locate' occurs!";
document.getElementById("demo26").innerHTML = text_4.indexOf("locate");
</script>
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.
<p id="demo27">
<script>
let text_5 = "Please locate where 'locate' occurs!";
document.getElementById("demo27").innerHTML = text_5.lastIndexOf("locate");
</script>
Both indexOf(), and lastIndexOf() return -1 if the text is not found.
<p id="demo28"></p>
<script>
let text_6 = "Please locate where 'locate' occurs!";
document.getElementById("demo28").innerHTML = text_6.indexOf("John");
</script>
Both methods accept a second parameter as the starting position for the search.
<p id="demo29"></p>
<script>
let text_7 = "Please locate where 'locate' occurs!";
document.getElementById("demo29").innerHTML = text_7.indexOf("locate",15);
</script>
The lastIndexOf() methods searches backwards (from the end to the beginning), meaning: if the second parameter is 15, the search starts at position 15, and searches to the beginning of the string.
<p id="demo30"></p>
<script>
let text_8 = "Please locate where 'locate' occurs!";
document.getElementById("demo30").innerHTML = text_8.lastIndexOf("locate",15);
</script>
The search() method searches a string for a specified value and returns the position of the match.
<p id="demo31"></p>
<script>
let text_9 = "Please locate where 'locate' occurs!";
document.getElementById("demo31").innerHTML = text_9.search("locate");
</script>
The match() method searches a match against a regular expression, and returns the matches, as an Array object.
<p id="demo32"></p>
<script>
let text_10 = "The rain in SPAIN stays mainly in the plain";
document.getElementById("demo32").innerHTML = text_10.match(/ain/g);
</script>
Perform a global, case-insensitive search for "ain".
<p id="demo33"></p>
<script>
let text_11 = "The rain in SPAIN stays mainly in the plain";
document.getElementById("demo33").innerHTML = text_11.match(/ain/gi);
</script>
The matchAll() method returns an iterator containing the results of matching a string against a string (or a regular expression).
<p id="demo52"></p>
<script>
let text29 = "I love cats. Cats are very easy to love. Cats are very popular.";
const iterator = text29.matchAll("Cats");
document.getElementById("demo52").innerHTML = Array.from(iterator);
</script>
If the parameter is a regular expression, the global flag (g) must be set set, otherwise a TypeError is thrown.
<p id="demo53"></p>
<script>
let text30 = "I love cats. Cats are very easy to love. Cats are very popular.";
const iterator1 = text30.matchAll(/Cats/g);
document.getElementById("demo53").innerHTML = Array.from(iterator1);
</script>
If you want to search case insensitive, the insensitive flag (i) must be set.
<p id="demo54"></p>
<script>
let text31 = "I love cats. Cats are very easy to love. Cats are very popular.";
const iterator2 = text31.matchAll(/Cats/gi);
document.getElementById("demo54").innerHTML = Array.from(iterator2);
</script>
The includes() method returns true if a string contains a specified value.
<p id="demo34"></p>
<script>
let text_12 = "Hello world, welcome to the universe.";
document.getElementById("demo34").innerHTML = text_12.includes("world");
</script>
A second argument indicates the starting position of the search.
<p id="demo35"></p>
<script>
let text_13 = "Hello world, welcome to the universe.";
document.getElementById("demo35").innerHTML = text_13.includes("world",12);
</script>
The startsWith() method returns true if a string begins with a specified value, otherwise false.
<p id="demo36"></p>
<script>
let text_14 = "Hello world, welcome to the universe.";
document.getElementById("demo36").innerHTML = text_14.startsWith("Hello");
</script>
<p id="demo38"></p>
<script>
let text_15 = "Hello world, welcome to the universe.";
document.getElementById("demo38").innerHTML = text_15.startsWith("world");
</script>
A second argument indicates the starting position of the search.
<p id="demo39"></p>
<script>
let text_16 = "Hello world, welcome to the universe.";
document.getElementById("demo39").innerHTML = text_16.startsWith("world",5);
</script>
<p id="demo40"></p>
<script>
let text_17 = "Hello world, welcome to the universe.";
document.getElementById("demo40").innerHTML = text_17.startsWith("world",6);
</script>
The endsWith() method returns true if a string ends with a specified value, otherwise false.
<p id="demo41"></p>
<script>
let text_18 = "John Doe";
document.getElementById("demo41").innerHTML = text_18.endsWith("Doe");
</script>
Check if the 11 first characters of a string ends with "world".
<p id="demo42"></p>
<script>
let text_19 = "Hello world, welcome to the universe.";
document.getElementById("demo42").innerHTML = text_19.endsWith("world", 11);
</script>
With template literals, you can use both single and double quotes inside a string.
<p id="demo43"></p>
<script>
let text_20 = `He's often called "Johnny"`;
document.getElementById("demo43").innerHTML = text_20;
</script>
Template literals allows multiline strings.
<p id="demo44"></p>
<script>
let text_21 =
`The quick
brown fox
jumps over
the lazy dog`;
document.getElementById("demo44").innerHTML = text_21;
</script>
Template literals allows variables in strings.
<p id="demo45"></p>
<script>
let firstName = "John";
let lastName = "Doe";
let text_22 =`Welcome ${firstName} ${lastName}!`;
document.getElementById("demo45").innerHTML = text_22;
</script>
Template literals allow expressions in strings.
<p id="demo46"></p>
<script>
let price = 10;
let VAT = 0.25;
let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;
document.getElementById("demo46").innerHTML = total;
</script>
Automatic replacing of expressions with real values is called string interpolation.
<p id="demo47"></p>
<script>
let header = "Template Literals";
let tags = ["template literals", "javascript", "es6"];
let html = `<h4>${header}</h4><ul>`;
for (const x of tags) {
html += `<li>${x}</li>`;
}
html += `</ul>`;
document.getElementById("demo47").innerHTML = html;
</script>