Aashman Verma
Aashman Verma's Blog

Follow

Aashman Verma's Blog

Follow
Working with strings ๐Ÿ˜€

Working with strings ๐Ÿ˜€

Aashman Verma's photo
Aashman Verma
ยทFeb 8, 2023ยท

3 min read

Accessing individual characters

We can treat the string as an array of characters so we can access individual characters with the help of index values starting from zero.

const str = "Hello";
console.log(str[0]); // returns H
console.log(str[1]); // returns e
console.log(str[2]); // returns l

.indexOf()

It returns the value of the index of the character which we enter into the function

const str1 = "Bye";
console.log(str1.indexOf("y")); // returns 1
console.log(str1.indexOf("B")); // returns 0

const str2 = "Squirrel";
console.log(str2.indexOf("r")); // returns 4

In the case of repeating characters, the function returns the index value of the first match.

.length

This function returns the length of the string or array.

const a = "Hello";
console.log(a.length); // returns 5

.trim()

It removes the excessive space from the start and end of the string. It doesn't mutate the string.

const b = "   Hello    "
console.log(b); // returns "   Hello   "
console.log(b.trim()); // returns "Hello"

.replace()

It replaces the first character or word matched keeping the rest the same. It doesn't mutate the string.

const x = "Hello"
console.log(x.replace("H","b")); // returns bello
console.log(x);
console.log(x.replace("l","b")); // returns Heblo

.replaceAll( ) function is similar to .replace( ) the only difference is that it replaces all the matching characters.

.slice()

We can use it to slice down a particular part of a string with the help of starting and ending index. It doesn't mutates the original string.

The syntax is string.slice(starting index, ending index), however element at the ending index is not included. The ending index is by default the length of the string if it is not specified. The negative index includes elements from the end of the string starting from -1.

const y = "Welcome to new project";
console.log(y.slice(0,4)); // returns Welc 
console.log(y.slice(1)); // returns elcome to new project
console.log(y.slice(-2)); // returns ct
console.log(y.slice(1,-1)); // returns elcome to new projec

.splice() function is similar to the .slice() function the only key difference is that the .splice() function mutates the original string or an array.

.split()

In strings, it is used to splits a string into an array of substrings and return the array. The identifier which is passed into () is used to split the string. It doesn't mutate the original string.

const greet = "Hello how are you";
const greetArr = greet.split(" ");
console.log(greetArr); // returns ["Hello","how","are","you"]

.join()

In strings, it is used to combine an array into strings separated by the symbol that we pass into the join function. It returns a string that we can store in a variable.

const newArr = ["Welcome","back","home"];
const newStr = newArr.join("+");
console.log(newStr); // returns Welcome+back+home

.padStart

The padStart() method in JavaScript is used to pad a string with another string until it reaches the given length. The padding is applied from the left end of the string.

Its syntax is : string.padStart(target length, padString)

const s1 = "This is js blog";
const s2 = s1.padStart(40,"yo");
console.log(s2); // returns yoyoyThis is js blog

We have a similar function to the above-named .padEnd() which does the same job but at the end of the string.

Js series link - js buddy


Follow up -

In case of any queries comment below :)

Twitter ๐Ÿ˜€

ย 
Share this