Short notes on JavaScript strings
Strings are very useful for holding data that can be represented in text form. Like other programming languages, strings are used to represent and manipulate a sequence of characters in JavaScript.
Some Strings methods and description:
charAt() :-
Returns the character at the specified index (position)
return 'cat'.charAt(2) // returns "t"return 'dog'.charAt(0) // returns "d"
concat():-
Joins two or more strings, and returns a new joined
const str1 = "Ariful";
const str2 = "Sagor";console.log(str1.concat(" ", str2));
// output: "Ariful Sagor"console.log(str2.concat(", ", str1));
// output: "Sagor, Ariful"
Checks whether a string contains the specified string/character
const sentence = 'The fox cut off it's tail.';const word = 'fox';console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);// output: "The word "fox" is in the sentence"
Checks whether a string ends with specified string/characters
const str1 = 'Dogs are worse';console.log(str1.endsWith('worse'));
// output: trueconst str2 = 'Is there any questions';console.log(str2.endsWith('?'));
// output: false
Returns the position of the last found occurrence of a specified value in a string
const paragraph = 'This is earth, a great planet.';const searchTerm = 'planet';console.log(`The index of the first "${searchTerm}" from the end is ${paragraph.lastIndexOf(searchTerm)}`);
// output: "The index of the first "planet" from the end is 23"
Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced
const p = 'Is that a dog?';console.log(p.replace('dog', 'monkey'));// expected output: "Is that a monkey?"
Extracts a part of a string and returns a new string
const str = 'The quick brown fox jumps over the lazy dog.';console.log(str.slice(31));
// output: "the lazy dog."console.log(str.slice(4, 19));
// output: "quick brown fox"console.log(str.slice(-4));
// output: "dog."console.log(str.slice(-9, -5));
// output: "lazy"
split():-
Splits a string into an array of substrings
const str = 'The horse ran very fast';const words = str.split(' ');
console.log(words[3]);
// output: "very"const chars = str.split('');
console.log(chars[8]);
// output: "e"
Checks whether a string begins with specified characters
const str1 = 'Lets go tomorrow.';console.log(str1.startsWith('Let'));
// output: trueconsole.log(str1.startsWith('Sat', 3));
// output: false
Converts a string to lowercase letters, according to the host’s locale
const sentence = 'What a beautiful bird that is!';console.log(sentence.toLowerCase());
// output: "what a beautiful bird that is!"
Converts a string to uppercase letters, according to the host’s locale
const sentence = 'The quick brown fox jumped very fast.';console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPED VERY FAST."
trim():-
Removes whitespace from both ends of a string
const greeting = ' I'm Sagor ';console.log(greeting);
// output: " I'm Sagor ";console.log(greeting.trim());
// output: "I'm Sagor";
Removes whitespace from end of a string
const greeting = ' I'm Sagor ';console.log(greeting);
// expected output: " I'm Sagor ";console.log(greeting.trimEnd());
// expected output: " I'm Sagor";
Removes whitespace from the beginning of a string
const greeting = ' I'm Sagor ';console.log(greeting);
// expected output: " I'm Sagor ";console.log(greeting.trimStart());
// expected output: "I'm Sagor ";
Usually a JavaScript developer has to work with all these tools almost every days. There are also more strings available in JavaScript. All these makes JavaScript a very suitable for developers to use in everyday work.