JavaScript String Methods

 JavaScript String Methods Cheat Sheet

javascript string methods cheat sheet
javascript string methods cheat sheet

1} charAt()

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

var str = 'Bhavesh Ajani';
console.log(str.charAt(0));
//Output: B

2} 
concat()

  • The concat() method joins two or more strings.
  • concat() does not change the existing strings, but returns a new string.
  • Example:
var strA = 'Bhavesh';
var strB = 'Ajani';
console.log(strA.concat(strB));
//Output: BhaveshAjani

3} indexOf()

  • The indexOf() method returns the position of the first occurrence of a specified value in a string.
  • indexOf() returns -1 if the value is not found.
  • indexOf() is case sensitive.
  • Example:
var str = 'Bhavesh';
console.log(str.indexOf('B'));
//Output: 0
console.log(str.indexOf('b'));
//Output: -1

4} lastIndexOf()

  • The lastIndexOf() method returns the position of the last occurrence of a specified value in a string.
  • lastIndexOf() searches the string from the end to the beginning.
  • lastIndexOf() returns -1 if the value is not found.
  • lastIndexOf() is case sensitive.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.lastIndexOf('a'));
//Output: 10
console.log(str.lastIndexOf('A'));
//Output: 8		
console.log(str.lastIndexOf('z'));
//Output: -1	

5} 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.
  • replace() does not change the original string.
  • Example:
var str = 'Bhavesh Adani';
console.log(str.replace('Adani', 'Ajani'));
//Output: Bhavesh Ajani

6} search()

  • The search() method searches a string for a specified value, and returns the position of the match.
  • The search value can be string or a regular expression.
  • The search() method returns -1 if no match is found.
  • Example:
var str = 'Bhavesh Ajani';	
console.log(str.search('Ajani'));		
//Output: 8
console.log(str.search('Adani'));		
//Output: -1

7} slice(start,end)

  • The slice() method extracts parts of a string and returns the extracted parts in a new string.
  • Use the start and end parameters to specify the part of the string you want to extract.
  • The first character has the position 0, the second has position 1, and so on.
  • Use a negative number to select from the end of the string.
  • the end position does not include, it extracts strings like end_position - 1.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.slice(0, 7));
//Output: Bhavesh

8} substr(start,length)

  • The substr() method extracts parts of a string, beginning at the character at a specified position, and returns a specified number of characters.
  • If you extract characters from the end of the string, use a negative start number.
  • substr() method does not change the original string.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.substr(0, 9));
//Output: Bhavesh A

9} substring(start,end)

  • The substring() method extracts characters between "start" and "end", not including "end".
  • The substring() method does not change the original string.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.substring(0, 7));
//Output: Bhavesh

10} toUpperCase()

  • The toUpperCase() method converts a string to uppercase letters.
  • The toUpperCase() method does not change the original string.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.toUpperCase());
//Output: BHAVESH AJANI

11} valueOf()

  • The valueOf() method returns the value of a string.
  • It is used to return the primitive value of a number.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.valueOf());
//Output: Bhavesh Ajani

12} trim()

  • The trim() method removes whitespace from both sides of a string.
  • The trim() method does not change the original string.
  • Example:
var str = ' Bhavesh Ajani ';
console.log(str.trim());
//Output: Bhavesh Ajani

13} toString()
  • The toString() method returns the value of a string.
  • The toString() method converts a number to a string.
  • Example:
var str = 7;
console.log(str.toString());
//Output: 7

14} includes()
  • The includes() method returns true if a string contains a specified string, otherwise false.
  • includes() method determines whether a string contains the given characters within it or not.
  • includes() is case sensitive.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.includes('ajani'));
//Output: false
console.log(str.includes('Ajani'));
//Output: true

15} charCodeAt()
  • The charCodeAt() method returns the Unicode of the character at the specified index in a string.
  • The index of the first character is 0, the second character 1, and so on.
  • The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.charCodeAt(2));
//Output: 97
console.log(str.charCodeAt(0));
//Output: 66

16} match()
  • The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
  • This method returns null if no match is found.
  • Example:
var str = 'Bhavesh Ajani AJANI';
console.log(str.match(/Ajani/g));
//Output: [ 'Ajani' ]

console.log(str.match(/ajani/g));
//Output: null

console.log(str.match(/ni/gi));
//Output: [ 'ni', 'NI' ]
  • Here “g” flag indicates that the regular expression should be tested against all possible matches in a string. 
17} split()
  • The split() method splits a string into an array of substrings, and returns the new array.
  • If an empty string ("") is used as the separator, the string is split between each character.
  • The split() method does not change the original string.
  • Example:
var strA = '1,2,3,4,5,6,7';
console.log(strA.split(","));
//Output:   [ '1', '2', '3', '4', '5', '6', '7' ]

var strB = 'Bhavesh Ajani';
console.log(strB.split(" "));
//Output:   [ 'Bhavesh', 'Ajani' ]

var strC = 'My Name Is Bhavesh';
console.log(strC.split(" ", 2));
//Output:   [ 'My', 'Name' ]

18} toLowerCase()
  • The toLowerCase() method converts a string to lowercase letters.
  • The toLowerCase() method does not change the original string.
  • Example:
var str = 'Bhavesh Ajani';
console.log(str.toLocaleLowerCase());
//Output:   bhavesh ajani

Thanks for reading this blog. If you have any query you can ask me. Let me know if you liked the blog or made a mistake.

Thank you guys, please comment and share.

Comments

Post a Comment