Day 5 : how many String Methods in javascript with example ?

Jemin KikaniJemin Kikani
5 min read

● String length

● String slice()

● String substring()

● String substr()

● String replace()

● String replaceAll()

● String toUpperCase() ● String toLowerCase() ● String concat()

String Length:-

● String trim() ● String trimStart()

● String trimEnd() ● String padStart()

● String padEnd() ● String charAt()

● String charCodeAt() ● String split()

The length property returns the length of a string.

Example:-

let text = “Hello World”;

let length = text.length; // length = 11

String Slice():-

slice() extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: start position, and end position (end not included).

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.slice(7, 13); // Banana

Notes:- JavaScript counts positions from zero.

1. If you omit the second parameter, the method will slice out the rest of the string.

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.slice(7); // Banana, Kiwi

2. If a parameter is negative, the position is counted from the end of the string.

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.slice(-12); // Banana, Kiwi

let text = “Apple, Banana, Kiwi”;

let length = text.slice(-12,-6); // Banana

String substring():-

substring() is similar to slice().

The difference is that start and end values less than 0 are treated as 0 in substring().

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.substring(7, 13); // Banana

String substr():-

substr() is similar to slice().

The difference is that the second parameter specifies the length of the extracted part.

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.substr(7, 6); // Banana

1. If you omit the second parameter, substr() will slice out the rest of the string.

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.substr(7); // Banana, Kiwi

2. If the first parameter is negative, the position is counted from the end of the string.

Example:-

let text = “Apple, Banana, Kiwi”;

let length = text.substr(-4); // Kiwi

String replace():-

The replace() method replaces a specified value with another value in a string Example:-

let text = “Please visit Microsoft!”;

let length = text.replace("Microsoft", "SkillQode");

Notes:- 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.

The replace() method is case sensitive.

String replaceAll():-

The replaceAll() method allows you to specify a regular expression instead of a string to be replaced.

Example:-

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");

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

String toUpperCase():-

The toUpperCase() method converts string lower case to upper case. Example:-

let text1 = "Hello World!";

let text2 = text1.toUpperCase(); // HELLO WORLD!

String toLowerCase():-

The toUpperCase() method converts string upper case to lower case. Example:-

let text1 = "Hello World!";

let text2 = text1.toLowerCase(); // hello world!

String concat():-

The concat() joins two strings.

Example:-

let text1 = "Hello";

let text2 = "World";

let text3 = text1.concat(“ ”, text2); // Hello World

The concat() method can be used instead of the plus operator.

String trim():-

The trim() method removes whitespace from both sides of a string. Example:-

let text1 = " Hello World! ";

let text2 = text1.trim(); // Hello world!

String trimStart():-

The trimStart() method removes like trim(), but removes whitespace only from the start of a string.

Example:-

let text1 = " Hello World! ";

let text2 = text1.trim(); // Hello world!

String trimEnd():-

The trimEnd() method removes like trim(), but removes whitespace only from the end of a string.

Example:-

let text1 = " Hello World! ";

let text2 = text1.trim(); // Hello world!

String padStart():-

The padStart() method pads a string with another string.

Example:-

let text = "5";

let padded = text.padStart(4,"0"); // 0005

To pad a number, convert the number to a string first.

Example:-

let num = 5;

let text = num.toString();

let padded = text.padStart(4,0); // 0005

String padEnd():-

The padEnd() method pads a string with another string.

Example:-

let text = "5";

let padded = text.padEnd(4,"0"); // 5000

To pad a number, convert the number to a string first.

Example:-

let num = 5;

let text = num.toString();

let padded = text.padEnd(4,0); // 5000

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(0); // H

String charCodeAt():-

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). Example:-

let text = "HELLO WORLD";

let char = text.charCodeAt(0); // 72

String split():-

A string can be converted to an array with the split() method. Example:-

let text = "HELLO WORLD";

let char = text.split(",") // Split on commas let char = text.split(" ") // Split on spaces let char = text.split("|") // Split on pipe

Maths Object

The JavaScript Math object allows you to perform mathematical tasks on numbers.

Example:-

Math.PI;

The Math object is static.

All methods and properties can be used without creating a Math object first.

Math Properties(Constants):-

The syntax for any Math property is : Math.property

JavaScript provides 8 mathematical constants that can be accessed as Math properties.

Example:-

Math.E // returns Euler's number

Math.PI // returns PI

Math.SQRT2 // returns the square root of 2

Math.SQRT1_2 // returns the square root of 1/2

Math.LN2 // returns the natural logarithm of 2

Math.LN10 // returns the natural logarithm of 10

Math.LOG2E // returns base 2 logarithm of E

Math.LOG10E // returns base 10 logarithm of E

0
Subscribe to my newsletter

Read articles from Jemin Kikani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jemin Kikani
Jemin Kikani