JavaScript String Methods


JavaScript string methods :


charAt()

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.

var str = "HELLO WORLD"; var result = str.charAt(0); console.log(result) //OUTPUT: H

charCodeAt()

The charCodeAt() method returns the Unicode of the character at the specified index in a string.

var str = "HELLO WORLD"; var result = str.charCodeAt(0); console.log(result) //OUTPUT: 72

concat()

The concat() method is used to join two or more strings. This method does not change the existing strings, but returns a new string containing the text of the joined strings.

var str1 = "HELLO "; var str2 = "WORLD"; var result = str1.concat(str2); console.log(result) //OUTPUT:HELLO WORLD

endsWith()

The endsWith() method determines whether a string ends with the characters of a specified string. This method returns true if the string ends with the characters, and false if not.

var str = "HELLO WORLD,I like Coding"; var result = str.endsWith("Coding"); console.log(result) //OUTPUT: true

fromCharCode()

The fromCharCode() method converts Unicode values into characters. This is a static method of the String object, and the syntax is always String. fromCharCode()

var result = str.fromCharCode(65); console.log(result) //OUTPUT: A

includes()

The includes() method determines whether a string contains the characters of a specified string.

var str = "HELLO WORLD,I like Coding"; var result = str.includes("like"); console.log(result) //OUTPUT: true

indexOf()

The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs.

var str = "HELLO WORLD, I like Coding"; var result = str.indexOf("I"); console.log(result) //OUTPUT: 13

lastIndexOf()

The lastIndexOf() method returns the position of the last occurrence of a specified value in a string. The string is searched from the end to the beginning, but returns the index starting at the beginning, at position 0.

var str = "HELLO WORLD, I like Coding"; var result = str.lastIndexOf("Coding"); console.log(result) //OUTPUT: 20

localeCompare()

The localeCompare() method compares two strings in the current locale. The locale is based on the language settings of the browser.

  • Returns -1 if str1 is sorted before str2
  • Returns 0 if the two strings are equal
  • Returns 1 if str1 is sorted after str2
var str1 = "ab"; var str2 = "cd"; var result = str1.localeCompare(str2); console.log(result) //OUTPUT: -1

match()

The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.

var str = "The rain in SPAIN stays mainly in the plain"; var result = str.match(/ain/g); console.log(result) //OUTPUT: ["ain", "ain" "ain"]

repeat()

The repeat() method returns a new string with a specified number of copies of the string it was called on.

var str = "NAMASTE."; var result = str.repeat(2); console.log(result) //OUTPUT: NAMASTE.NAMASTE.

replace()

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

var str = "I Like Coding"; var result = str.replace("Like", "Love"); console.log(result) //OUTPUT:I Love Coding

search()

The search() method searches a string for a specified value, and returns the position of the match.

var str = "I Like Coding"; var result = str.search("Like"); console.log(result) //OUTPUT: 2

JavaScript string methods :


slice()

The slice() method extracts parts of a string and returns the extracted parts in a new string.

var str = "HELLO WORLD"; var result = str.slice(0, 5); console.log(result) //OUTPUT: HELLO

split()

The split() method is used to split a string into an array of substrings, and returns the new array.

var str = "HOW ARE YOU DOING TODAY?"; var result = str.split(" "); console.log(result) //OUTPUT: ["HOW", "ARE", "YOU", "DOING", "TODAY?" ]

startsWith()

The startsWith() method determines whether a string begins with the characters of a specified string.

var str = "HOW ARE YOU DOING TODAY?"; var result = str.startsWith("HOW"); console.log(result) //OUTPUT: true

substr()

The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

var str = "HOW ARE YOU?"; var result = str.substr(4,6); console.log(result) //OUTPUT: ARE

substring()

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.

var str = "HOW ARE YOU DOING TODAY?"; var result = str.substring(4, 6); console.log(result) //OUTPUT: AR

toLocaleLowerCase()

The toLocaleLowerCase() method converts a string to lowercase letters, according to the host’s current locale.

var str = "Hello World!"; var result = str.toLocaleLowerCase(); console.log(result) //OUTPUT: hello world!

toLocaleUpperCase()

The toLocaleUpperCase() method converts a string to uppercase letters, according to the host’s current locale.

var str = "Hello World!"; var result = str.toLocaleUpperCase(); console.log(result) //OUTPUT: HELLO WORLD!

toLowerCase()

The toLowerCase() method converts a string to lowercase letters. It does not change the original string.

var str = "Hello World!"; var result = str.toLowerCase( ); console.log(result) //OUTPUT: hello world!

toUpperCase()

The toUpperCase() method converts a string to uppercase letters. It does not change the original string.

var str = "Hello World!"; var result = str.toUpperCase( ); console.log(result) //OUTPUT: HELLO WORLD!

trim()

The trim() method removes whitespace from both sides of a string. It does not change the original string.

var str = " Hello World! "; console.log(str); //OUTPUT: " Hello World! " var result = str.trim( ); console.log(result) //OUTPUT: Hello World!

I hope you like this JavaScript string methods article.

Also Read :