STRING
A string is a sequence of characters stored in variables and it is enclosed within the single and double quote. A JavaScript string is zero or more characters written inside quotes.
EXAMPLE
// In Double Quotes
let name = "string";
let str = "my name is sam";
// In Single Quotes
let name='string';
let str='my name is sam';
But always remember we cannot use the same quotes inside the string to highlight or describe some special characters.
EXAMPLE
let name=" He said, "my name is sam" "; // this will throw the error
How Can We Avoid This?
SOLUTION 1.
Use two different quotes that are single and double quotes in one string to avoid this scenario.
EXAMPLE
let name='He said,"my name is sam" ';
let name1="He live in 'Pune' ";
console.log(name);
console.log(name1);
// He said,"my name is sam"
// He live in 'Pune'
SOLUTION 2.
There is another way to avoid this scenario, this is by using escape character() in our string.
Example
let name="my name is \"sujit\" ";
console.log(name);
// my name is "sujit".
JavaScript Properties and String Methods
.length
This property returns the total length of the string.
let alpha="ABCDEF";
console.log(alpha.length);
// 6 (the length of the string)
.toUpperCase() and .toLowerCase
This method will transform text into uppercase or in lowercase.
let name="My name is SAM";
let upperText=name.toUpperCase();
let lowerText=name.toLowerCase();
console.log(upperText);
console.log(lowerText);
// MY NAME IS SAM
// my name is sam
replace()
This method replaces the specified value with another value in the string.
let name="My name is SAM";
let newName=name.replace("SAM","RAM");
console.log(newName);
// My name is RAM
replaceAll()
The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement.
let text=" my name is Ram, I got this name from Lord Ram ";
let newText=text.replaceAll("Ram","Rudra");
console.log(newText);
// " my name is Rudra, I got this name from Lord Rudra "
charAt()
The String object's charAt() method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.
let text=" my name is Ram, I got this name from Lord Ram ";
let index=text.charAt(9);
console.log(index);
// i (the spaces are also counted as a character)
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). slice(indexStart,indexEnd)
const text=" he is the most popular";
const texxt = "Apple, Banana, Kiwi";
let part = text.slice(-12,-6)
let text1=text.slice(1,10);
let text2=text.slice(1);
let text3=texxt.slice(-12,-6);
console.log(text1);
console.log(text2);
console.log(text3);
// "he is the"
// "he is the most popular" (here, we are not declared the ending index that's why it printed the whole string)
// Banana
substring()
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
const str="Ravan";
console.log(str.substring(1,3));
// av
split()
The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
const str="He fights like a lion";
const arr=str.split(' ');
console.log(arr);
// ["He", "fights", "like", "a", "lion"]
concat()
The concat() method concatenates the string arguments to the calling string and returns a new string.
const name="the name is ";
const type="Dev";
console.log(name.concat(type));
// "the name is Dev"
trim()
The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.
const name=" my name is Dev ";
console.log(name.trim());
// "my name is Dev"
trimEnd()
The trimEnd() method removes whitespace from the end of a string and returns a new string, without modifying the original string.
const name=" my name is Dev ";
console.log(name.trimEnd());
// " my name is Dev"
trimStart()
The trimStart() method removes whitespace from the beginning of a string and returns a new string, without modifying the original string.
const name=" my name is Dev ";
console.log(name.trimStart());
// "my name is Dev "
That's the wrap. I Hope, you enjoyed the Blog. Leave your Reaction and Comment.