The Fun Continues on Javascript String Operations
Previously, we’ve seen Javascript string operations like finding the length of the string, Combining strings, and Substring methods. Please check that article before reading this.
Today, we are going to see some unique string operations in JavaScript.
https://athiyagu6.medium.com/exploring-fun-with-javascript-string-operations-e2bc099abc0c
Slicing String
Slicing and substring are the same. These two methods are handy when splitting a string. In substring, we can’t use the negative values. Position values are assumed to be zero, even less than zero.
let message = "I can do this all day";
let subMessage = message.substring(0,5);
let sliceMessage = message.slice(0,8);
console.log(sliceMessage);//outputs -- I can do --
console.log(subMessage);//outputs -- I can --
But slicing takes negative values as well. Negative values start from right to left in a string.
let message = "I can do this all day";
let sliceNeg = message.slice(-1);
let subNeg = message.substring(-1);
console.log(sliceNeg);//outputs --y--
console.log(subNeg);//outputs --I can do this all day --
Substr is also used to split the string, rather than using start and end positions. We use the start position and length of the desired part.
let message = "I can do this all day";
let substrMessage = message.substr(0,3);//second parameter is length
console.log(substrMessage);//outputs -- I c--
Changing the Case of The String
Javascript strings have two methods to convert the text into a preferred case.
let text = "hello, MAN";
let lowerText = text.toLowerCase();
let upperText = text.toUpperCase();
console.log(lowerText);//outputs -- hello, man --
console.log(upperText);//outputs -- HELLO,MAN --
Trimming the String
Trim is a Javascript operation specifically for strings. That removes whitespaces in both ends of the given text. We can control the trimming operation using trim start(trimStart) and trim end(trimEnd).
let message = " Avengers Assemble ";
let trimmed = message.trim();
console.log(trimmed);//outputs --Avengers Assemble--
Trim start only removes whitespace on the left side of the text. The trim end deletes the right side of the text.
let message = " Avengers Assemble ";
let leftTrim = message.trimStart();
console.log(leftTrim);//outputs --Avengers Assemble --
let rightTrim = message.trimEnd();
console.log(rightTrim);//outputs -- Avengers Assemble--
Please try the length operation to see the difference. These changes are not visible in the console.
ReplaceAll
We’ve already seen the string replace operation in the previous post. This replace-all method changes characters equal to the given text.
let sms = "my cat eats dog food";
let replacedSms = sms.replaceAll("cat","dog");
console.log(replacedSms); //outputs -- my dog eats dog food--
Some Least Used String Operations
Repeat:
This method repeats a given string multiple times. The required number of repeats can be mentioned as a parameter.
let message = "Moshi";
let text = message.repeat(3);
console.log(text);//outputs -- MoshiMoshiMoshi --
Padding
The padding method adds text to the given string. The pad-start method adds text in the beginning. Padding stops when the length of the string reaches the provided number.
let price = "5";
let message = price.padStart(3,'O')
console.log(message);//outputs -- OO5 --
Let’s wind up for today. We will meet in another Javascript article to learn more. Thank you for reading. Follow now to receive upcoming article updates.