HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
String methods help you to work with strings
Primitive values, like "John Doe", cannot have properties or methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
The length
property returns the length of a string:
let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = txt.length;
There are 3 methods for extracting a part of a string:
slice(start, end)
substring(start, end)
substr(start, length)
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 (end not included).
Slice out a portion of a string from position 7 to position 13 (13 not included):
let str = "Apple, Banana, Kiwi";
let part = str.slice(7, 13);
JavaScript counts positions from zero.
First position is 0.
Second position is 1.
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:
let str = "Apple, Banana, Kiwi";
let part = str.slice(-12, -6);
If you omit the second parameter, the method will slice out the rest of the string:
let part = str.slice(7);
or, counting from the end:
let part = str.slice(-12);
substring()
is similar to slice()
.
The difference is that start and end values less than 0 are treated as 0 in
substring()
.
let str = "Apple, Banana, Kiwi";
let part = str.substring(7, 13);
If you omit the second parameter, substring()
will slice out the rest of the
string.
substr()
is similar to slice()
.
The difference is that the second parameter specifies the length of the extracted part.
let str = "Apple, Banana, Kiwi";
let part = str.substr(7, 6);
If you omit the second parameter, substr()
will slice out the rest of the
string.
let str = "Apple, Banana, Kiwi";
let part = str.substr(7);
If the first parameter is negative, the position counts from the end of the string.
let str = "Apple, Banana, Kiwi";
let part = str.substr(-4);
The replace()
method replaces a specified value with another
value in a string:
let text = "Please visit Microsoft!";
let newText = text.replace("Microsoft", "w3resource");
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.
By default, the replace()
method replaces only the first match:
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace("Microsoft", "w3resource");
By default, the replace()
method is case sensitive. Writing MICROSOFT (with
upper-case) will not work:
let text = "Please visit Microsoft!";
let newText = text.replace("MICROSOFT", "w3resource");
To replace case insensitive, use a regular expression with an /i
flag (insensitive):
let text = "Please visit Microsoft!";
let newText = text.replace(/MICROSOFT/i, "w3resource");
Regular expressions are written without quotes.
To replace all matches, use a regular expression with a /g
flag (global match):
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace(/Microsoft/g, "w3resource");
You will learn a lot more about regular expressions in the chapter JavaScript Regular Expressions.
A string is converted to upper case with toUpperCase()
:
A string is converted to lower case with toLowerCase()
:
let text1 = "Hello World!";
let text2 = text1.toUpperCase();
let text1 = "Hello World!"; // String
let text2 = text1.toLowerCase(); // text2 is text1
converted to lower
concat()
joins two or more strings:
let text1 = "Hello";
let text2 = "World";
let text3 = text1.concat(" ", text2);
The concat()
method can be used instead of the plus operator.
These two lines do the same:
text = "Hello" + " " + "World!";
text = "Hello".concat(" ", "World!");
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.
The trim()
method removes whitespace from both sides of a string:
let text1 = " Hello World! ";
let text2 = text1.trim();
ECMAScript 2019 added the String method trimStart()
to JavaScript.
The trimStart()
method works like trim()
, but removes whitespace only from the start of a string.
let text1 = " Hello World! ";
let text2 = text1.trimStart();
JavaScript String trimStart()
is supported in all browsers since January 2020:
Chrome 66 | Edge 79 | Firefox 61 | Safari 12 | Opera 50 |
Apr 2018 | Jan 2020 | Jun 2018 | Sep 2018 | May 2018 |
ECMAScript 2019 added the String method trimEnd()
to JavaScript.
The trimEnd()
method works like trim()
, but removes whitespace only from the end of a string.
let text1 = " Hello World! ";
let text2 = text1.trimEnd();
JavaScript String trimEnd()
is supported in all browsers since January 2020:
Chrome 66 | Edge 79 | Firefox 61 | Safari 12 | Opera 50 |
Apr 2018 | Jan 2020 | Jun 2018 | Sep 2018 | May 2018 |
ECMAScript 2017 added two String methods: padStart()
and padEnd()
to support padding at the beginning and at the end of a string.
The padStart()
method pads a string with another string:
let text = "5";
let padded = text.padStart(4,"x");
let text = "5";
let padded = text.padStart(4,"0");
The padStart()
method is a string method.
To pad a number, convert the number to a string first.
See the example below.
let numb = 5;
let text = numb.toString();
let padded = text.padStart(4,"0");
padStart()
is an ECMAScript 2017 feature.
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
padStart()
is not supported in Internet Explorer.
The padEnd()
method pads a string with another string:
let text = "5";
let padded = text.padEnd(4,"x");
let text = "5";
let padded = text.padEnd(4,"0");
The padEnd()
method is a string method.
To pad a number, convert the number to a string first.
See the example below.
let numb = 5;
let text = numb.toString();
let padded = text.padEnd(4,"0");
padEnd()
is an ECMAScript 2017 feature.
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
padEnd()
is not supported in Internet Explorer.
There are 3 methods for extracting string characters:
charAt(position)
charCodeAt(position)
The charAt()
method returns the character at a specified
index (position) in a string:
let text = "HELLO WORLD";
let char = text.charAt(0);
The charCodeAt()
method returns the unicode of the character
at a specified index in a string:
The method returns a UTF-16 code (an integer between 0 and 65535).
let text = "HELLO WORLD";
let char = text.charCodeAt(0);
ECMAScript 5 (2009) allows property access [ ] on strings:
let text = "HELLO WORLD";
let char = text[0];
Property access might be a little unpredictable:
let text = "HELLO WORLD";
text[0] = "A"; // Gives no error, but does not work
If you want to work with a string as an array, you can convert it to an array.
A string can be converted to an array with the split()
method:
text.split(",") // Split on commas
text.split(" ") // Split on spaces
text.split("|") // Split on pipe
If the separator is omitted, the returned array will contain the whole string in index [0].
If the separator is "", the returned array will be an array of single characters:
text.split("")
For a complete String reference, go to our:
Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.
Convert the text into an UPPERCASE text:
let txt = "Hello World!"; txt = txt.;