0% found this document useful (0 votes)
4 views8 pages

JavaScript String Manipulation

The document provides an overview of JavaScript string manipulation, including basic string methods, substring extraction, searching, modifying, combining, and splitting strings. It highlights various methods such as length, charAt, indexOf, slice, includes, toUpperCase, and template literals with examples. The content serves as a guide for understanding and utilizing string operations in JavaScript.

Uploaded by

anshik004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

JavaScript String Manipulation

The document provides an overview of JavaScript string manipulation, including basic string methods, substring extraction, searching, modifying, combining, and splitting strings. It highlights various methods such as length, charAt, indexOf, slice, includes, toUpperCase, and template literals with examples. The content serves as a guide for understanding and utilizing string operations in JavaScript.

Uploaded by

anshik004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript String

Manipulation
A JAVASCRIPT STRING IS ZERO OR MORE CHARACTERS WRITTEN INSIDE
QUOTES.

YOU CAN USE SINGLE OR DOUBLE QUOTES: ‘ ‘ , “ “


 let name= “Nielit Kurukshetra“ ;

 let str =‘NIELIT’;


Basic String Methods

Method Description Example

Returns the length of the


length "hello".length → 5
string

Returns the character at a


charAt(index) "hello".charAt(1) → "e"
specified index

Returns the index of the first


indexOf(substring) "hello".indexOf("l") → 2
occurrence

Returns the index of the last


lastIndexOf(substring) "hello".lastIndexOf("l") → 3
occurrence
✂️Extracting Substrings

Method Description Example

slice(start, end) Extracts a part of a string "hello".slice(1, 4) → "ell"

Similar to slice but no


substring(start, end) "hello".substring(1, 4) → "ell"
negative indexes

Extracts part of a string based


substr(start, length) "hello".substr(1, 3) → "ell"
on length
🔍 Searching in Strings

Method Description Example

includes(substring) Checks if substring exists "hello".includes("he") → true

Checks if string starts with


startsWith(substring) "hello".startsWith("he") → true
value

Checks if string ends with


endsWith(substring) "hello".endsWith("lo") → true
value
🔄 Modifying Strings

Method Description Example

toUpperCase() Converts to uppercase "hello".toUpperCase() → "HELLO"

toLowerCase() Converts to lowercase "HELLO".toLowerCase() → "hello"

Removes whitespace from both


trim() " hello ".trim() → "hello"
ends

"hello world".replace("world",
replace(old, new) Replaces first occurrence
"friend") → "hello friend"

"a-b-a".replaceAll("a", "x") → "x-b-


replaceAll(old, new) Replaces all occurrences
x"
🧱 Combining and Splitting Strings

Method Description Example

"Hello".concat(" ", "World") →


concat() Joins two or more strings
"Hello World"

repeat(n) Repeats the string n times "ha".repeat(3) → "hahaha"


Template Strings / Template Literals

 let firstName = "John";


 let lastName = "Doe";

 let text = `Welcome ${firstName}, ${lastName}!`;

You might also like