ABOUT CONTACT
Prev Page Next Page
Top

JavaScript String Methods

➢String methods help you to work with strings.
➢Primitive values, like "John Doe", cannot have properties or methods (because they are not objects).
➢But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.

JavaScript String Length

➢The length property returns the length of a string:

EXAMPLE:

let Name = "Manu";
let length = Name.length

Extracting String Parts

There are 3 methods for extracting a part of a string:
➢slice(start, end) ➢substring(start, end) ➢substr(start, length)

JavaScript String slice()

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

EXAMPLE:

let Name = "Manu, Sunny, Sanvi";
let part = str.slice(7,13);

JavaScript 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 Name = "Manu, Sunny, Sanvi";
let part = str.substring(7,13);

JavaScript String substr()

➢substring() is similar to slice().
➢The difference is that the second parameter specifies the length of the extracted part.

EXAMPLE:

let Name = "Manu, Sunny, Sanvi";
let part = str.substr(7,13);

Replacing String Content

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

EXAMPLE:

let Name = "Please visit Google!";
let NewName = Name.replace("Google","BMR EDUCATION");

Converting to Upper and Lower Case

➢A string is converted to upper case with toUpperCase():
➢A string is converted to lower case with toLowerCase():

EXAMPLE:

let Name = "Manu";
let Name2 = Name1.toUpperCase();(7,13);

EXAMPLE:

let Name = "Manu";
let Name2 = Name1.toLowerCase();(7,13);

JavaScript String concat()

➢concat() joins two or more strings:

EXAMPLE:

let Name1 = "Manogna";
let Name2 = "Reddy";
let Name3 = Name1.concat(" ", Name2);

JavaScript String trim()

➢The trim() method removes whitespace from both sides of a string:

EXAMPLE:

let Name1 = "      Manogna Reddy      ";
let Name2 = Name1.trim();

JavaScript String trimStart()

➢ECMAScript 2019 added the String method trimStart() to JavaScript.
➢The trimStart() method works like trim(), but removes whitespace only from the start of a string.

EXAMPLE:

let Name1 = "      Manogna Reddy      ";
let Name2 = Name1.trimStart();

JavaScript String trimEnd()

➢ECMAScript 2019 added the String method trimEnd() to JavaScript.
➢The trimEnd() method works like trim(), but removes whitespace only from the end of a string.

EXAMPLE:

let Name1 = "      Manogna Reddy      ";
let Name2 = Name1.trimEnd();

JavaScript String Padding

➢ECMAScript 2017 added two String methods: padStart() and padEnd() to support padding at the beginning and at the end of a string.

JavaScript String padStart()

➢The padStart() method pads a string with another string:

EXAMPLE:

let Num = "10";
let padded = Num.padStart(4 ,"x");

JavaScript String padEnd()

➢The padEnd() method pads a string with another string:

EXAMPLE:

let Num = "10";
let padded = Num.padEnd(4 ,"x");

Extracting String Characters

There are 3 methods for extracting string characters:
➢charAt(position)
➢charCodeAt(position)
➢Property access [ ]

JavaScript String charAt()

➢The charAt() method returns the character at a specified index (position) in a string:

let Name = "Susindra";
let char = Name.charAt(0);

JavaScript 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 Name = "Susindra";
let char = Name.charCodeAt(0);

Property Access

➢ECMAScript 5 (2009) allows property access [ ] on strings:

EXAMPLE:

let Name = "Susindra";
let char = Name[0];

Converting a String to an Array

➢If you want to work with a string as an array, you can convert it to an array.

JavaScript String split()

➢A string can be converted to an array with the split() method:

EXAMPLE:

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

Prev Page Next Page