Loading Please Wait...
JavaScript strings methods are used to perform various operations on string.
The length property returns the length of a string.
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let len = text.length; // len = 26
The 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).
let text = "Lynxsia IT Solutions";
let part1 = text.slice(7, 15);
// Only start position till the rest of the string
let part2 = text.slice(7);
// start from the end (start not included)
let part3 = text.slice(-7);
// start and end from the end
let part3 = text.slice(-7, -5);
The substring() is similar to slice(). The difference is that start and end values less than 0 are treated as 0 in substring().
let text = "Lynxsia IT Solutions";
let part = text.substring(7, 15);
let part2 = text.substring(-7, 15); // treated as (0, 15)
The substr() is similar to slice(). The difference is that the second parameter specifies the length of the extracted part.
let text = "Lynxsia IT Solutions";
let part1 = text.substr(7, 6);
// from 7 to rest
let part2 = text.substr(7);
// count from end
let part3 = text.substr(-7);
let part3 = text.substr(-7, 3);
The replace() method replaces a specified value with another value in a string. It replaces only the first match.
let text = "Lynxsia it Solutions";
let part = text.replace('it', 'IT');
The replaceAll() method replaces a specified value with another value in a string. It replaces all matches.
let text = "Lynxsia it Solutions";
let part = text.replaceAll('it', 'IT');
The toUpperCase() method convert string to uppercase.
let text = "Lynxsia it Solutions";
let part = text.toUpperCase();
The toLowerCase() method convert string to lowercase.
let text = "Lynxsia it Solutions";
let part = text.toLowerCase();
The concat() joins two or more strings.
let text1 = "Lynxsia";
let text2 = "IT Solutions";
let text3 = text1.concat(" ", text2);
The trim() method removes whitespace from both sides of a string.
The trimStart() method removes whitespace only from the start of a string.
The trimEnd() method removes whitespace only from the end of a string.
let text = " Lynxsia IT Solutions ";
let text2 = text.trim();
let text3 = text.trimStart();
let text4 = text.trimEnd();
The padStart() method pads a string from the start. It pads a string with another string (multiple times) until it reaches a given length.
The padEnd() method pads a string from the end. It pads a string with another string (multiple times) until it reaches a given length.
padStart() and padEnd() methods are string methods. To pad a number, convert the number to a string first.
let text = "5";
let padStart = text.padStart(4,"0");
let padEnd = text.padStart(4,"0");
The charAt() method returns the character at a specified index (position) in a string.
The charCodeAt() method returns the unicode of the character at a specified index in a string.
let text = "5";
let charPos = text.charAt(0);
let charPosCode = text.charCodeAt(0);
A string can be converted to an array with the split() method.
text.split(",") // Split on commas
text.split(" ") // Split on spaces
text.split("|") // Split on pipe
ECMAScript 5 (2009) allows property access [ ] on strings. Property access might be a little unpredictable.
It makes strings look like arrays (but they are not). If no character is found, [ ] returns undefined, while charAt() returns an empty string.
It is read only. str[0] = "A" gives no error (but does not work!).
let text = "HELLO WORLD";
let char = text[0];
text[0] = "A"; // Gives no error, but does not work
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved