Javascript String Methods with example


Javascript String Methods with example

In this tutorial, you will learn the javascript string methods with examples one by one as given below

  1. length:- This property returns the length of the given string which means it counts every character of the string and returns the total number of characters in the string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph returns the length of the given string</p>
	<p id="demo"></p>
<script>
	let text = "Helloworld";
	document.getElementById("demo").innerHTML = text.length;
</script>
</body>
</html>

Output:-

Below paragraph returns the length of the given string

10

Also read, Object in Javascript with example

slice():- This function extracts some part of the given string and returns the extracted string. This string method takes two parameters as the start position index and the end position index. Please note that the start position index starts from zero and the end position index can not take place in the extracted string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph extracts a part of the string and returns the extracted string</p>
	<p id="demo1"></p>
<script>
	let text = "Helloworld";
	document.getElementById("demo1").innerHTML = text.slice(0,5);
</script>
</body>
</html>

Output:-

Below paragraph extracts a part of the string and returns the extracted string

Hello

substring():- This function is also as same as the slice() function and returns the extracted string from the given string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph extracts a part of the string and returns the extracted string</p>
	<p id="demo2"></p>
<script>
	let text = "Helloworld";
	document.getElementById("demo2").innerHTML = text.substring(0,5);
</script>

</body>
</html>

Output:-

Below paragraph extracts a part of the string and returns the extracted string

Hello

substr( ):- This function is also similar to the substring() function and takes exactly two parameters and returns the extracted string from the given string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph extracts a part of the string and returns the extracted string</p>
	<p id="demo3"></p>
<script>
	let text = "Helloworld";
	document.getElementById("demo3").innerHTML = text.substr(0,5);
</script>
</body>
</html>

Output:-

Below paragraph extracts a part of the string and returns the extracted string

Hello

replace():- This function replaces some part of the string with a new string inside the given string. This function takes exactly two parameters as given below

  • The first parameter defines the text to be replaced
  • The second parameter defines the replaced text

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph replaces a part of the string with a new string</p>
	<p id="demo4"></p>
<script>
	let text = "Helloworld";
	document.getElementById("demo4").innerHTML = text.replace("world","India");
</script>
</body>
</html>

Output:-

Below paragraph replaces a part of the string with a new string

HelloIndia

replace( ) with g:- This function replaces all the matching parts of the given string with a new string. This function takes exactly two parameters as given below

  • The first parameter defines all the matching text to be replaced.
  • The second parameter defines the replaced text.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph replaces all the matching parts of the string with a new string</p>
	<p id="demo5"></p>
<script>
	let string = "If you want to visit Inida then you should visit the capital of India";
	document.getElementById("demo5").innerHTML = string.replace(/you/g,"I");
</script>
</body>
</html>

Output:-

Below paragraph replaces all the matching parts of the string with a new string

If I want to visit India then I should visit the capital of India

toUpperCase( ):- This function converts all the characters of the given string into uppercase. This function does not require any parameter.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph makes the given string in uppercase.</p>
	<p id="demo6"></p>
<script>
	let string = "If you want to visit Inida then you should visit the capital of India";
	document.getElementById("demo6").innerHTML = string.toUpperCase();
</script>
</body>
</html>

Output:-

Below paragraph makes the given string in uppercase.

IF YOU WANT TO VISIT INIDA THEN YOU SHOULD VISIT THE CAPITAL OF INDIA

toLowerCase():- This function converts all the characters of the given string into lowercase. This function also does not require any parameter.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
    <p>Below paragraph makes the given string in lowercase.</p>
	<p id="demo7"></p>
<script>
	let string = "If you want to visit Inida then you should visit the capital of India";
	document.getElementById("demo7").innerHTML = string.toLowerCase();
</script>
</body>
</html>

Output:-

Below paragraph makes the given string in lowercase.

if you want to visit inida then you should visit the capital of india

trim( ):- This function removes or eliminates all the whitespaces found in the given string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
    <p>Below paragraph removes whitespaces from the both side of a string</p>
	<p id="demo9"></p>
<script>
	let trimstring = "Helloworld";
	document.getElementById("demo9").innerHTML = trimstring.trim();
</script>
</body>
</html>

Output:-

Below paragraph removes whitespaces from the both side of a string

Helloworld

charAt():- This function will return the searched character from the given string. Suppose we want to search for a particular character from the given string then this function will help us to find that character in the given string. It starts counting the character from position zero.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph will diplay the searched character from a string</p>
	<p id="demo10"></p>
<script>
	let text = "Helloworld";
    document.getElementById("demo10").innerHTML = text.charAt(5);
</script>
</body>
</html>

Output:-

Below paragraph will diplay the searched character from a string

w

charCodeAt():- This function will return the Unicode number of characters from the given string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph will diplay the unicode character from a string</p>
	<p id="demo11"></p>
<script>
	let text = "Helloworld";
	document.getElementById("demo11").innerHTML = text.charCodeAt(5);
</script>
</body>
</html>

Output:-

Below paragraph will diplay the unicode character from a string

119

padStart():- This function appends a new string at the start of the given string. This function takes exactly two parameters as given below

  • The first parameter defines the number of times the new string is to be added
  • The second parameter defines the new string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
    <p>Below paragraph will pads a string  at the start with another string</p>
	<p id="demo12"></p>
<script>
	let padstring = 3;
	let padnumberstring  = padstring.toString();
	document.getElementById("demo12").innerHTML = padnumberstring.padStart(5,'y');
</script>
</body>
</html>

Output:-

Below paragraph will pads a string at the start with another string

yyyy3

padEnd():- This function appends a new string at the end of the given string. This function takes exactly two parameters as given below

  • The first parameter defines the number of times the new string is to be added.
  • The second parameter defines the new string.

Note:- If the given string is a number then you should convert it to a string first using the toString() function in both the padStart() and padEnd() methods.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph will pads a string  at the end with another string</p>
	<p id="demo13"></p>
<script>
	let padstring = 3;
	let padnumberstring  = padstring.toString();
	document.getElementById("demo13").innerHTML = padnumberstring.padEnd(5,'y');
</script>
</body>
</html>

Output:-

Below paragraph will pad a string at the end with another string

3yyyy

concat():- This method appends a new string with the given string.

Example:-

<html>
<head>
<title>Javascript string methods</title>
</head>
<body>
	<p>Below paragraph will join two strings together</p>
	<p id="demo14"></p>
<script>
	let text1 = "Hello";
	let text2 = "World";
    document.getElementById("demo14").innerHTML = text1.concat(" ",text2);
</script>
</body>
</html>

Output:-

Below paragraph will join two strings together

Hello World

Conclusion: I Hope this tutorial will help you to understand the concept of javascript string methods. If there is any doubt then please leave a comment below.


Leave a Comment

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

THOR311

KUPU178

RATU311

THOR311

THOR311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

THOR311

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

THOR311

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

THOR311

RATU311

THOR311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311