0% found this document useful (0 votes)
7 views

Javascript Strings Methods

The document summarizes common JavaScript string methods including length(), toLowerCase(), toUpperCase(), indexOf(), substring(), split(), replace(), and trim(). These methods allow you to work with strings by getting string lengths, converting case, finding indexes, extracting substrings, splitting into arrays, replacing values, and removing whitespace.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Javascript Strings Methods

The document summarizes common JavaScript string methods including length(), toLowerCase(), toUpperCase(), indexOf(), substring(), split(), replace(), and trim(). These methods allow you to work with strings by getting string lengths, converting case, finding indexes, extracting substrings, splitting into arrays, replacing values, and removing whitespace.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JavaScript String

Methods

. length()
.toLowerCase()

.toUpperCase()

.indexOf()

..,.___ _ _...,. .substring()


String
. split()

.replace()

. trim()
1. length
The length property returns the number of
characters in a string. I

n the example above, it returns the length of


the string "Robert" which is 6 characters.

1 let name = "Robert" ;


2
3 console. log (name.length);
4 I I Output: 6
2. tolowerCase()
The tolowerCase() method converts all
characters in a string to lowercase.

In this example, it transforms the string


"Hello World" to "hello world".

l Let text = "Hello World" ;


2 let lowerlext = text. tolowerCase ();
3
4 console. log (lowerlext);
5 / / Output: "hellc t-JOr'ld"
3. toUpperCaseO
The toUpperCase() method converts all
characters in a string to uppercase.

In this example, it transforms the string


"Hello World" to "HELLO WORLD".

1 Let text = "Hello World";


2 let upperlext = text. toUpperCase ();
3
4 console. log (upperText);
5 // Output: "HELLO WORLD"
4. indexofQ
The indexOf () method finds the first
occurrence of a specified value in a string
and returns its position.

In this example, it returns the position of the


string "how" in the message, which is 7.

1 let message = "Hello, how are you?" ;


2 Let position = message. indexOf ( "how" );
3
4 console. log(position);
5 I/ Output : 7
5. substring()
The substring() method extracts a part of a
string between two specified positions and
returns a new string.

In this example, it extracts the string "love"


from the original message.

1 let message = "I love JavaScript" ;


2 let partialMessage = message. substring{2, 6);
3
4 console. log (partialMessage);
5 // Output: "love''
6. splitO
The split() method divides a string into an
array of substrings based on a specified
delimiter.

In this example, it splits the message into an


array of words using the space character as
the delimiter.

1 Let message = "I am learning JavaScript" ;


2 Let words = message. split ( "" );
3
4 console. log (words);
5 u u t put : l "I " , " .:m1 " , " 1 le' a r' n in g " , " J av c=i Sc r, i pt " ]
7. replaceQ
The replace() method searches a string for
a specified value and replaces it with a new
value.

In this example, it replaces the word "cats"


with "dogs" in the message.

l Let message = "I love cats" ;


2 Let newMessage = message. replace ( "cats" , "dogs" );
3
4 console. log (newMessage);
5 Output: "I lovf:::> dogs
8. trimQ
The trim() method removes whitespace
from both the beginning and end of a string.

In this example, it trims the extra spaces


before and after the text "Hello, world!".

1 let input = " Hello, world!


2 let trimmedlnput = input. trim ();
3
4 console. log (trimmedlnput);
5 /1 Out~)ut: "He11oJ v,;0 1'lci!"

You might also like