➢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.
➢The length property returns the length of a string:
EXAMPLE:
let Name = "Manu";
let length = Name.length
There are 3 methods for extracting a part of a string:
➢slice(start, end)
➢substring(start, end)
➢substr(start, length)
➢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);
➢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);
➢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);
➢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");
➢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);
➢concat() joins two or more strings:
EXAMPLE:
let Name1 = "Manogna";
let Name2 = "Reddy";
let Name3 = Name1.concat(" ", Name2);
➢The trim() method removes whitespace from both sides of a string:
EXAMPLE:
let Name1 = " Manogna Reddy ";
let Name2 = Name1.trim();
➢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();
➢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();
➢ECMAScript 2017 added two String methods: padStart() and padEnd() to support padding at the beginning and at the end of a string.
➢The padStart() method pads a string with another string:
EXAMPLE:
let Num = "10";
let padded = Num.padStart(4 ,"x");
➢The padEnd() method pads a string with another string:
EXAMPLE:
let Num = "10";
let padded = Num.padEnd(4 ,"x");
There are 3 methods for extracting string characters:
➢charAt(position)
➢charCodeAt(position)
➢Property access [ ]
➢The charAt() method returns the character at a specified index (position) in a string:
let Name = "Susindra";
let char = Name.charAt(0);
➢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);
➢ECMAScript 5 (2009) allows property access [ ] on strings:
EXAMPLE:
let Name = "Susindra";
let char = Name[0];
➢If you want to work with a string as an array, you can convert it to an array.
➢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