String Manipulation Techniques in Javascript

Top Javascript most useful String Methods

Javascript being one of the most powerful language helps us with built-in functions to do many string operations. We can use these built-in functions instead of making functions from scratch, it makes them easy for use in our projects.

Lets learn one by one string functions along with proper examples.

String Concatenation:

There are 2 ways to do it:

i. One way is to use concat function.

let str1 = "Hello";
let str2 = " world";
let result = str1.concat(str2);
console.log(result); // Output: Hello world

ii. Another way is to use ‘+’ operator.

let str1 = "Hello";
let str2 = "world";
let result = str1 + " " + str2;
console.log(result); // Output: Hello world

Joining Strings:

  • If you have array of strings, we can join them by a specific separator using join function.
const arr = ['Hello', 'World'];
const res = arr.join(' '); // 'Hello Bob'

String Length:

  • To get the length of a string, you can use the length property.
let str = "Hello world";
console.log(str.length); // Output: 11

String Templates:

  • ES6 or later versions has template strings so you can include strings within another string, like this
const hello = "Hello ";
const world = "World!";
const res = `${hello} ${world}`; // Hello Bob!

String Trimming:

  • To remove whitespace from both ends of a string, you can use the trim() method.
let str = "    Hello world    ";
console.log(str.trim()); // Output: Hello world

Remove all Whitespaces from a String:

  • There is no direct built-in function to remove all whitespace from a string. We have to replace the spaces in the string with empty strings.
const str = "         Hello World!     ";
const res = str.replace(/ /g, ''); // 'HelloWorld!'

Substrings:

  • To extract a part of a string, you can use the substring() method.
let str = "Hello world";
let result = str.substring(0, 5);
console.log(result); // Output: Hello

String Cases:

  1. ToLowerCase()

  2. ToUpperCase()

  • To convert a string to uppercase or lowercase, you can use the toUpperCase() and toLowerCase() methods respectively.
let str = "Hello world";
console.log(str.toUpperCase()); // Output: HELLO WORLD
console.log(str.toLowerCase()); // Output: hello world

String Search:

There are many ways to search for strings in JavaScript.

By using startsWith, endsWith, indexOf, lastIndexOf, charAt, search, and includes functions.

startsWith:

startsWith checks if a string starts with particularly given substring you pass in or not and returns a boolean value.

For example:

const str = "Hello world.";
const hasHello = str.startsWith("Hello"); // true
const hasHello2 = str.startsWith("abc"); // false

endsWith:

endsWith checks if a string ends with the given substring you pass in or not and returns a boolean value.

For example:

const str = "Hello world.";
const hasHello = str.endsWith("world."); // true
const hasHello2 = str.endsWith("abc"); // false

indexOf:

indexOf finds the index of the first occurrence of the substring in a given string and returns -1 if not found.

For example:

const str = "Hello Hello.";
const hasHello = str.indexOf("Hello"); // 0
const hasHello2 = str.indexOf("abc"); // -1

lastIndexOf:

lastIndexOf finds the index of the last occurrence of the substring in a given string and returns -1 if not found.

For example:

const str = "Hello Hello.";
const hasHello = str.lastIndexOf("Hello"); // 6
const hasHello2 = str.lastIndexOf("abc"); // -1

charAt:

charAt returns the character located at the given index of the string.

For example:

const str = "Hello";
const res = str.charAt(0); // 'H'

search:

search gets the position of the substring passed into the function. It returns -1 if the substring is not found in the string.

For Example:

const str = "Hello World";
const res = str.search('e'); // 1

includes:

includes checks if the passed-in substring is in the string. Returns true if it is in the string, false otherwise.

For Example:

const str = "Hello World";
const hasH = str.includes('o'); // true
const hasW = str.includes('z'); // false

String Replace:

  • To replace a substring within a string, you can use the replace() method.
let str = "Hello world";
console.log(str.replace("world", "JavaScript")); // Output: Hello JavaScript

Thanks for reading📖! I hope you enjoyed😀 reading this article.

Keep smiling!

Have a nice day!

Did you find this article valuable?

Support Hemanth Raju by becoming a sponsor. Any amount is appreciated!