JavaScript String Methods
String length
String slice()
String substring()
String substr()
String replace()
String replaceAll()
String toUpperCase()
String toLowerCase()
String concat()
|
String trim()
String trimStart()
String trimEnd()
String padStart()
String padEnd()
String charAt()
String charCodeAt()
String split()
|
Note :
String search methods are covered in the next chapter.
JavaScript String Length
The length
property tells you how long a string is.
Extracting String Characters
There are 4 methods for extracting string characters:
Extracting String Parts
There are three ways to get a piece of text:
- The
at(position)
Method
- The
charAt(position)
Method
- The
charCodeAt(position)
Method
- Using property access [] like in arrays
JavaScript String charAt()
The charAt()
function gives you the character found at a particular position in a string.
JavaScript String charCodeAt()
The charCodeAt()
method gives the code of a character in a string at a certain position.
The function gives back a number between 0 and 65535, known as a UTF-16 code.
JavaScript String at()
In ES2022, a new feature was added for strings called the at()
method.
The at()
method gives you the letter at a certain position in a sentence.
The at()
method works on all new web browsers starting from March 2022.
Note
The at()
method is something new in JavaScript.
It lets you use negative numbers to find characters, whereas charAt()
doesn't.
Now you can write myString.at(-2)
instead of charAt(myString.length-2)
.
Property Access [ ]
Note :
Accessing property values could sometimes be somewhat uncertain.
- It transforms strings to resemble arrays, though they remain distinct entities.
- If there's no character, [ ] shows nothing, but charAt() shows an empty space.
Extracting String Parts
slice(start, end)
substring(start, end)
substr(start, length)
JavaScript String slice()
The slice()
function takes a piece of text (a string) and gives you back just that piece in a new string.
The method takes 2 parameters: start position, and end position (end not included).
Note :
JavaScript counts positions from zero.
First position is 0.
Second position is 1.
JavaScript String substring()
substring()
is much like slice()
.
The change here is that if you have numbers less than 0 for the start and end values in the "substring()" function, they will be considered as 0.
If you don't include the second part, substring()
will take everything from that point till the end of the string.
JavaScript String substr()
The code substr()
works much like slice()
.
The thing to note here is that the second parameter tells us how long the extracted part should be.
If you don't provide a second value, substr()
will cut out the remaining part of the string.
If the initial value is a negative number, then the counting starts from the last part of the string.
Converting to Upper and Lower Case
You can make a string all uppercase by using the toUpperCase()
function.
You can change a string to lowercase using toLowerCase()
.
JavaScript String toUpperCase()
JavaScript String toLowerCase()
JavaScript String concat()
The concat()
function combines two or more strings together.
You can use the concat()
method instead of the plus sign (+).
These two lines achieve the same result:
Example
text = "Hello" + " " + "World!";
text = "Hello".concat(" ", "World!");
Note
All string methods return a new string. They don't modify the original string.
Formally said:
Strings are immutable: Strings cannot be changed, only replaced.
JavaScript String trim()
The trim()
function takes a string and gets rid of any extra spaces at the beginning and end of it.
JavaScript String trimStart()
In 2019, a new update to JavaScript called "ECMAScript 2019" introduced a new function for working with strings. This function is called "trimStart()" and it helps you remove spaces or white spaces from the beginning of a string.
The trimStart()
function is similar to trim()
, but it only takes away spaces from the beginning of a text.
JavaScript String trimEnd()
In 2019, ECMAScript introduced a new function called `trimEnd()` to JavaScript.
The trimEnd()
function is similar to trim()
, but it only erases empty spaces at the end of a text.
JavaScript String Padding
ECMAScript 2017 added two new string methods to JavaScript: padStart()
and padEnd()
to support padding at the beginning and at the end of a string.
JavaScript String padStart()
The padStart()
function adds extra characters to the beginning of a text.
This code adds more of one string to another string until the second string becomes a specific length.
Note :
The padStart()
method is a string method.
To pad a number, convert the number to a string first.
See the example below.
JavaScript String padEnd()
The padEnd()
function adds extra characters to the end of a string.
It adds extra copies of one string to the end of another string until the resulting string is as long as you want it to be.
Note :
The padEnd()
method is a string method.
To pad a number, convert the number to a string first.
See the example below.
JavaScript String repeat()
The repeat()
method gives back a string containing multiple copies of another string.
The repeat()
method gives back a fresh string.
The repeat()
function doesn't alter the original text.
Replacing String Content
The replace()
method is used to swap one value with another value in a text.
Note :
The replace()
method does not change the string it is called on.
The replace()
method returns a new string.
The replace()
method replaces only the first match
If you want to replace all matches, use a regular expression with the /g flag set. See examples below.
The replace()
method usually changes just the initial match.
The standard behavior of the replace()
method is that it pays attention to letter case. So, if you try to replace "MICROSOFT" with uppercase letters, it won't succeed.
To make a comparison that doesn't care about capital letters, you can use a special kind of pattern called a 'regular expression' with a '/i' flag (which stands for 'insensitive'):
Note :
Regular expressions are written without quotes.
To change every occurrence, employ a 'regular expression' with a '/g' flag (for global matching):
JavaScript String ReplaceAll()
In 2021, JavaScript added a new function for working with text called replaceAll()
:
The replaceAll()
method lets you use a special code pattern instead of a plain text to replace something.
If you're using a regular expression as a parameter, make sure it has the 'g' (global) flag enabled. If it's not set, you'll encounter a TypeError.
Note :
replaceAll()
is an ES2021 feature.
replaceAll()
does not work in Internet Explorer.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The length Property</h2>
<p>The length of the string is:</p>
<p id="demo"></p>
<script>
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = text.length;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String</h1>
<h2>The charAt() Method</h2>
<p>The charAt() method returns the character at a given position in a string:</p>
<p id="demo"></p>
<script>
var text = "HELLO WORLD";
document.getElementById("demo").innerHTML = text.charAt(0);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String</h1>
<h2>The charCodeAt() Method</h2>
<p>The charCodeAt() method returns the unicode of the character at a given position in a string:</p>
<p id="demo"></p>
<script>
let text = "HELLO WORLD";
document.getElementById("demo").innerHTML = text.charCodeAt(0);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The at() Method</h2>
<p>The at() method returns an indexed element from a string:</p>
<p id="demo"></p>
<script>
const name = "W3Schools";
let letter = name.at(2);
document.getElementById("demo").innerHTML = letter;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>Bracket Notation</h2>
<p>The bracked notation [] returns an indexed element from a string:</p>
<p id="demo"></p>
<script>
const name = "W3Schools";
let letter = name[2];
document.getElementById("demo").innerHTML = letter;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>Property access on strings:</p>
<p>The first character in the string is:</p>
<p id="demo"></p>
<script>
let text = "HELLO WORLD";
document.getElementById("demo").innerHTML = text[0];
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>Property acces on strings are read only:</p>
<p id="demo"></p>
<script>
let text = "HELLO WORLD";
text[0] = "A"; // Does not work
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>
<p>The sliced (extracted) part of the string is:</p>
<p id="demo"></p>
<script>
let text = "Apple, Banana, Kiwi";
let part = text.slice(7, 13);
document.getElementById("demo").innerHTML = part;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>
<p>Extract a part of a string from position 7:</p>
<p id="demo"></p>
<script>
let text = "Apple, Banana, Kiwi";
let part = text.slice(7)
document.getElementById("demo").innerHTML = part;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>The substring() method takes out a section of a string and gives back that part as a new string.</p>
<p id="demo"></p>
<script>
let str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substring(7, 13);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>The substr() method extract a part of a string and returns the extracted parts in a new string:</p>
<p id="demo"></p>
<script>
let str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substr(7, 6);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>The substr() function takes a piece of a text and gives back that piece as a new text.</p>
<p id="demo"></p>
<script>
let str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substr(7);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>The substr() method takes out a section of words from a sentence and gives back those words as a separate group:</p>
<p id="demo"></p>
<script>
let str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substr(-4);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Convert string to upper case:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Hello World!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.toUpperCase();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Convert string to lower case:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Hello World!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.toLowerCase();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String</h1>
<h2>The concat() Method</h2>
<p>The concat() method joins two or more strings:</p>
<p id="demo"></p>
<script>
let text1 = "Hello";
let text2 = "World!";
let text3 = text1.concat(" ", text2);
document.getElementById("demo").innerHTML = text3;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The trim() Method</h2>
<p id="demo"></p>
<script>
let text1 = " Hello World! ";
let text2 = text1.trim();
document.getElementById("demo").innerHTML = "Length text1 = " + text1.length + " <br> Length text2 = " + text2.length;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The trimStart() Method</h2>
<p id="demo"></p>
<script>
let text1 = " Hello World! ";
let text2 = text1.trimStart();
document.getElementById("demo").innerHTML = "Length text1 = " + text1.length + " <br> Length text2 = " + text2.length;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The trimEnd() Method</h2>
<p id="demo"></p>
<script>
let text1 = " Hello World! ";
let text2 = text1.trimEnd();
document.getElementById("demo").innerHTML = "Length text1 = " + text1.length + " <br> Length text2 = " + text2.length;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The padStart() Method</h2>
<p>The padStart() method pads a string from the start.</p>
<p>It pads the string with another string (multiple times) until it reaches a given length.</p>
<p id="demo"></p>
<script>
let text = "5";
text = text.padStart(4, "0");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The padStart() Method</h2>
<p>The padStart() method pads a string from the start.</p>
<p>It adds more of one string to another string many times until it becomes as long as you want.</p>
<p id="demo"></p>
<script>
let numb = 5;
let text = numb.toString();
document.getElementById("demo").innerHTML = text.padStart(4, 0);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The padEnd() Method</h2>
<p>The padEnd() method pads a string at the end.</p>
<p>It pads the string with another string (multiple times) until it reaches a given length.</p>
<p id="demo"></p>
<script>
let text = "5";
text = text.padEnd(4, "0");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The padEnd() Method</h2>
<p>The padEnd() method pads a string at the end.</p>
<p>It pads the string with another string (multiple times) until it reaches a given length.</p>
<p id="demo"></p>
<script>
let numb = 5;
let text = numb.toString();
document.getElementById("demo").innerHTML = text.padEnd(4, "x");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The repeat() Method</h2>
<p>repeat() returns a new string with a number of copies of a string:</p>
<p id="demo"></p>
<p>repeat() is not supported in Internet Explorer.</p>
<script>
let text = "Hello world!";
let result = text.repeat(2);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The repeat() Method</h2>
<p>repeat() returns a new string with a number of copies of a string:</p>
<p id="demo"></p>
<p>repeat() is not supported in Internet Explorer.</p>
<script>
let text = "Hello world!";
let result = text.repeat(4);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Replace "Microsoft" with "Propertutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.replace("Microsoft", "Propertutorials");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Replace "Microsoft" with "Propertutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft and Microsoft!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.replace("Microsoft", "Propertutorials");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Try changing "Microsoft" to "Propertutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.replace("MICROSOFT", "Propertutorials");
}
</script>
<p>The replace() method is case sensitive. MICROSOFT (with upper-case) will not be replaced.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Replace "Microsoft" with "Propertutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.replace(/MICROSOFT/i, "Propertutorials");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p>Replace all occurrences of "Microsoft" with "Propertutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft and Microsoft!</p>
<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.replace(/Microsoft/g, "Propertutorials");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The replaceAll() Method</h2>
<p>ES2021 intoduced the string method replaceAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
text = text.replaceAll("Cats", "Dogs");
text = text.replaceAll("cats", "dogs");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The replaceAll() Method</h2>
<p>ES2021 intoduced the string method replaceAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular";
text = text.replaceAll(/Cats/g, "Dogs");
text = text.replaceAll(/cats/g, "dogs");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>