DEV Community

Cover image for JavaScript Common String Methods
Jehad Hossain
Jehad Hossain

Posted on

JavaScript Common String Methods

In this article I’m going to discuss with you about the string methods in JavaScript charAt, concat, includes, endsWith, indexOf, lastIndexOf, replace, slice, split, startsWith, substring, toLowercase, toUppercase, trim, trimStart, and trimEnd.

  • charAt(): It returns the text at the index which is given as the input.
const text = "hello world"
console.log(`text at the index 4 is ${text.charAt(4)}`);
Enter fullscreen mode Exit fullscreen mode
  • concat(): It combines all the given strings and returns a new string.
const vowels = "aeiou"
const consonents = "bcdfghjklmnpqrstvwxyz";
const word0 = "Hello world.
const word1 = " Hi world.";
const word2 = " Bye world.";
const alphabets = vowels.concat(consonents);
const sentence = word0.concat(word1, word2);
console.log(alphabets);
console.log(sentence);
Enter fullscreen mode Exit fullscreen mode
  • includes(): It tries to find the element which is given as input if it finds the element on the string it returns true if not then false.
const vowels = "aeiou

console.log(vowels.includes("u"));// true

console.log(vowels.includes("z"));// false

const consonents = "bcdfghjklmnpqrstvwxyz"

console.log(consonents.includes("h"))// true

console.log(consonents.includes("a"))// false"
Enter fullscreen mode Exit fullscreen mode
  • endsWith(): It checks the last character of a string. If it matches then it returns true if not false. You can give the last character or the last word to find or match.
const text = "hello world"

console.log(text.endsWith("d")); // true

console.log(text.endsWith("world")); // true

console.log(text.endsWith("l"));// false;
Enter fullscreen mode Exit fullscreen mode
  • indexOf(): It returns the index number of the given input if the input does not match then it returns -1. It takes two parameters the first one is the element and the second one is the starting index number which is optional.
const alphabets = "abcdefghijklmnpqrstuvwxyza"

console.log(alphabets.indexOf("i")); // output: 8

console.log(alphabets.indexOf("a", 1)); // output: 25

console.log(alphabets.indexOf("o")); // output: -1
Enter fullscreen mode Exit fullscreen mode
  • lastIndexOf(): It searches the string for the given input from backward of the string. It returns the index number of the matched element. If no element is matched it returns -1.
const alphabets = "abcdefghijklmnpqrstuvwxyza"

console.log(alphabets.lastIndexOf("i")); // output: 8

console.log(alphabets.lastIndexOf("a")); // output: 25

console.log(alphabets.lastIndexOf("o")); // output: -1;
Enter fullscreen mode Exit fullscreen mode
  • replace(): It replaces the given input from the string and returns a new string. It takes two parameters first one is which word to replace and the second parameter is what will be set on the replaced position if the second parameter is not given it returns undefined on the replaced position. You can use regex as the first input.
const text = "The quick brown Bear jumps over the lazy dog."

const regex = /Bear/;

console.log(text.replace("Bear", "goat"));

console.log(text.replace(regex, "lion"));;
Enter fullscreen mode Exit fullscreen mode
  • slice(): It returns a piece of text from a string. It takes two parameters one is the starting index and the second is the ending index. If you just give the first index then it will copy all the elements of a string after that index and return a new string. If you give a two-parameter then it will copy from the start index to the end index and return a new string.
const text = "The quick brown fox jumps over the lazy dog."

console.log(text.slice(10)); // output: brown fox jumps over the lazy dog.

console.log(text.slice(10, 20)); // output: brown fox

// if you use negative index then it will count from the end of string

console.log(text.slice(-9, -1)); // output: lazy dog
Enter fullscreen mode Exit fullscreen mode
  • split(): It divides the string by following the pattern which is given as input. Then it puts the divided parts into an array and returns the array.
const text = "The quick brown fox jumps over the lazy dog."

const words = "ab, eb, ib, ob, ub";

console.log(text.split(" "));
console.log(words.split("b"));
Enter fullscreen mode Exit fullscreen mode
  • startsWith(): It checks the string if it has started with the word or letter which has been given as the input to this method. If the word matches then it returns true if not then false.
const text = "The quick brown fox jumps over the lazy dog."

console.log(text.startsWith("T")); // output: true

console.log(text.startsWith("The")); // output: true

console.log(text.startsWith("A")); // output: false

console.log(text.startsWith("quick")); // output: false;
Enter fullscreen mode Exit fullscreen mode
  • substring(): It returns the part of a string. It takes two parameters first one is the starting index and the second one is the ending index. If you don’t give the second parameter then it will return everything after the starting index as a new string.
const text = "The quick brown fox jumps over the lazy dog."

console.log(text.substring(10)); // output: brown fox jumps over the lazy dog.

console.log(text.substring(4, 15)); // output: quick brown

// if you give greater value on the first parameter then it will automatically swap the parameters

console.log(text.substring(15, 4)); // output: quick brown;
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase(): Makes all the words of the calling string into lower case. Then returns them as a string.
const text = "The Quick Brown Fox Jumps Over The Lazy Dog."

console.log(text.toLowerCase());
Enter fullscreen mode Exit fullscreen mode
  • toUpperCase(): Makes all the words of the calling string into upper case. Then returns them as a string.
const text = "The quick brown fox jumps over the lazy dog."

console.log(text.toUpperCase());
Enter fullscreen mode Exit fullscreen mode
  • trim(): It removes all the white spaces from starting and ending of the calling string. And returns a new string.
const text = "      Hello world     "

console.log(text.trim()); // output: Hello world;
Enter fullscreen mode Exit fullscreen mode
  • trimStart(): It removes the white spaces from the beginning of the calling string. And returns a new string.
const text = "      Hello world     "
console.log(text.trimStart());;
Enter fullscreen mode Exit fullscreen mode
  • trimEnd(): It removes the white spaces from the end of the calling string. And returns a new string.
const text = "      Hello world     "
console.log(text.trimEnd());
Enter fullscreen mode Exit fullscreen mode

Discussion (0)