String Methods in JavaScript.

String Methods in JavaScript.

"Infinity is not just a number, it's a mindset. Embrace the chaos, and discover the boundless possibilities within yourself." - Rick Sanchez, Rick and Morty

Introduction

According to Mozilla Developer Network(MDN).

A string is used to represent and manipulate a sequence of characters.

Strings are objects that are used to represent text. Strings can be created by enclosing text within single quotes (''), double quotes ("") or back-ticks (``). Once created, strings can be manipulated and accessed using various methods in JavaScript. This document outlines the commonly used string methods in JavaScript, along with their descriptions and examples.

charAt()

This method returns a character at a specified index/position.

const myString = 'I am a software developer' 

console.log(myString.charAt(0)) //"I" 
console.log(myString.charAt(3)) //"m"

charCodeAt()

This method returns a Unicode of the character at a specific index

const myString = 'I am a software developer' 
console.log(myString.charCodeAt(0)) //73 
console.log(myString.charCodeAt(3)) //109

concat()

This method joins two or more strings and returns a new string

const myString = 'I am a software developer.' 
const secondString = 'Hire me'

console.log(myString.concat(secondString)) // I am a software developer.Hire me

endsWith()

This method Checks if a string ends with a specified string or character.

const myString = 'I am a software developer.'

console.log(myString.endsWith('software')) //false 
console.log(myString.endsWith('developer.')) //true

fromCharCode()

This method converts unicode values to characters

const myString = 'I am a software developer.'

console.log(String.fromCharCode(73)) //"I" 
console.log(String.fromCharCode(109)) //"m"

includes() This method checks if the string contains the specified string or character.

const myString = 'I am a software developer.'

console.log(myString.includes('y')) //false 
console.log(myString.includes('t')) //true

indexOf() This method returns the position of the first found occurrence of the specified value of a string.

const myString = 'I am a software developer.'

console.log(myString.indexOf('software')) // 7 
console.log(myString.indexOf('am')) // 2

lastIndexOf()

This method returns the position of the last found occurrence of the specified value of a string.

const thisString = 'I am a software developer.'

console.log(thisString.lastIndexOf('software')) // 7 
console.log(thisString.lastIndexOf('am')) // 2

match()

This method searches a string for a match against the regular expression and returns the matches. if it's more than one match, it returns an array.

const thisString = 'I am a software developer.'

console.log(thisString.match(/e/g)) // ['e', 'e', 'e', 'e'] 
console.log(thisString.match(/o/g)) // ['o', 'o']

repeat()

This method returns a new string with a specified number of copies of an existing string.

const thisString = 'Hire me'

console.log(thisString.repeat(3)) // Hire meHire meHire me

replace()

This method searches a string for a specified value or regular expression and returns a new string where the specified values are.

const thisString = 'I am a software developer.'

console.log(thisString.replace(/software/g , 'SOFTWARE')) // I am a SOFTWARE developer. 
console.log(thisString.replace(/developer/g , 'DEVELOPER')) // I am a software DEVELOPER.

search()

This method searches a string for a specified value or regular expression and returns the position of the match.

const thisString = 'I am a software developer.'

console.log(thisString.search('developer')) // 16 
console.log(thisString.search('am')) // 2

slice()

This method extracts a part of a string and returns a new string.

const thisString = 'I am a software developer.'

console.log(thisString.slice(2 , 4)) // am 
console.log(thisString.slice(10 , 14)) // twar

split()

This method splits a string into sub strings and returns an array.

const thisString = 'Hire me!'

console.log(thisString.split('')) // ['H', 'i', 'r', 'e', ' ', 'm', 'e', '!'] 
console.log(thisString.split(' ')) // ['Hire', 'me!']

startsWith()

This method returns a boolean if a string starts with a specified character.

const thisString = 'Hire me!'

console.log(thisString.startsWith('me')) // false 
console.log(thisString.startsWith('Hire')) // true

subStr()

This method extracts the character from a string beginning at a specified start position.

const thisString = 'I am a software developer.'

console.log(thisString.substring(2 , 4)) // am 
console.log(thisString.substring(10 , 14)) // twar

toLowerCase()

This method returns the string in lowercase.

const thisString = 'I am a software developer.'

console.log(thisString.toLowerCase()) // i am a software developer.

toUpperCase()

This method returns the string in upper case.

const thisString = 'I am a software developer.'

console.log(thisString.toUpperCase()) // I AM A SOFTWARE DEVELOPER.

trim()

This method removes whitespaces from either side of the string.

const thisString = ' I am a software developer. ' 
console.log(thisString.trim()) //I am a software developer.

Summary

This document covered the commonly used string methods in JavaScript, including their descriptions and examples. By using these methods, you can perform various operations on strings to manipulate, extract, or transform their contents.

Further Reading

By exploring the resources below, you can deepen your understanding of string methods in JavaScript, learn about their specific use cases, and discover more advanced techniques for working with strings.

  1. MDN Web Docs - String

  2. JavaScript.info - Strings

  3. JavaScript String Methods Cheat Sheet

  4. Eloquent JavaScript - Strings