0% found this document useful (0 votes)
1 views6 pages

Javascript String

The document provides an overview of JavaScript string methods, including how to create strings, extract characters, and search within strings. It details methods such as charAt(), charCodeAt(), indexOf(), lastIndexOf(), slice(), replace(), and replaceAll(), along with examples for each method. Additionally, it highlights the introduction of the at() method in ES2022 and the replaceAll() method in ES2021.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Javascript String

The document provides an overview of JavaScript string methods, including how to create strings, extract characters, and search within strings. It details methods such as charAt(), charCodeAt(), indexOf(), lastIndexOf(), slice(), replace(), and replaceAll(), along with examples for each method. Additionally, it highlights the introduction of the at() method in ES2022 and the replaceAll() method in ES2021.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript String Methods

Strings are for storing text

Strings are written with quotes


var text = `He's often called "Johnny"`;

JavaScript String Length

The length property returns the length of a string:


Example
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

Extracting String Characters

There are 4 methods for extracting string characters:

 The at(position) Method


 The charAt(position) Method
 The charCodeAt(position) Method
 Using property access [] like in arrays

JavaScript String charAt()

The charAt() method returns the character at a specified index (position) in a string:
Example
let text = "HELLO WORLD";
let char = text.charAt(6);

JavaScript String charCodeAt()

The charCodeAt() method returns the code of the character at a specified index in a string:

The method returns a UTF-16 code (an integer between 0 and 65535).
Example
let text = "HELLO WORLD";
let char = text.charCodeAt(0);
JavaScript String at()

ES2022 introduced the string method at():


Examples

Get the third letter of name:


const name = "W3Schools";
let letter = name.at(2);

Get the third letter of name:


const name = "W3Schools";
let letter = name[2];

The at() method returns the character at a specified index (position) in a string.

The at() method is supported in all modern browsers since March 2022:
JavaScript String Search
JavaScript String indexOf()

The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it
returns -1 if the string is not found:
Example
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
JavaScript String lastIndexOf()

The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
Example
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");

Both indexOf(), and lastIndexOf() return -1 if the text is not found:


Example
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("John");
JavaScript String search()

The search() method searches a string for a string (or a regular expression) and returns the position of
the match:
Examples
let text = "Please locate where 'locate' occurs!";
text.search("locate");
JavaScript String match()

The match() method returns an array containing the results of matching a string against a string (or a
regular expression).
Examples

Perform a search for "ain":


let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");

JavaScript String slice()


<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>
<p>slice() extracts a part of a string and returns the extracted part:</p>
<p id="demo"></p>
<script>
let text = "Hello world!";
let result = text.slice(0, 5);

document.getElementById("demo").innerHTML = result;
</script>

</body>
</html>
The slice() Method

slice() extracts a part of a string and returns the extracted part:

Hello

<html>
<body>

<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>

<p>slice() extracts a part of a string and returns the extracted part:</p>

<p id="demo"></p>

<script>
let text = "Hello world!";
let result = text.slice(3);

document.getElementById("demo").innerHTML = result;
</script>

</body>
</html>

The slice() Method

slice() extracts a part of a string and returns the extracted part:


lo world!

JavaScript String replace()

<html>

<body>

<h1>JavaScript Strings</h1>

<h2>The replace() Method</h2>

<p>replace() searches a string for a value,

and returns a new string with the specified value(s) replaced:</p>

<p id="demo">Visit Microsoft!</p>

<script>

let text = document.getElementById("demo").innerHTML;

document.getElementById("demo").innerHTML = text.replace("Microsoft", "W3Schools");

</script>

</body>

</html>

The replace() Method

replace() searches a string for a value, and returns a new string with the specified value(s) replaced:

Visit W3Schools!
JavaScript String replaceAll()

<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>
The replaceAll() Method

ES2021 intoduced the string method replaceAll().

I love dogs. Dogs are very easy to love. Dogs are very popular.

String indexOf()

<html>

<body>

<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>

<p>indexOf() returns the position of the first occurrence of a value in a string.</p>

<p>Find "welcome":</p>

<p id="demo"></p>

<script>

let text = "Hello world, welcome to the universe.";

let result = text.indexOf("welcome");

document.getElementById("demo").innerHTML = result;

</script>

</body>

</html>
The indexOf() Method

indexOf() returns the position of the first occurrence of a value in a string.

Find "welcome":

13

You might also like