You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
String.indexOf(searchString) takes a parameter searchString
and it searches the searchString inside the string starting from the start/left
-and returns the index value of the searchString from first occurrence if found, if not returns -1
if nothing is passed as a parameter, the parameter will be undefined
Examples
conststring='The code undefined code code'console.log(string.indexOf('code'))// 4console.log(string.indexOf('Code'))// -1console.log(string.indexOf(''))// 0console.log(string.indexOf())// 9 // since undefined automatically passed and 'undefined' is in the string and matched it.
3. lastIndexOf
String.lastIndexOf(searchString) takes a parameter searchString
and it searches the searchString inside the string starting from the right/end
and returns the index value of the searchString from last occurrence if found, if not returns -1
if nothing passed as a parameter, the parameter will be undefined
Examples
conststring="The code undefined code code";console.log(string.lastIndexOf("code"));// 24console.log(string.lastIndexOf("Code"));// -1console.log(string.lastIndexOf(""));// 28console.log(string.lastIndexOf());// 9 // since undefined automatically passed and 'undefined' is in the string and matched it.
4. slice
String.slice() method extracts a section of a string and returns as a new string so they don't change the original string
syntax
String.slice(indexStart, indexEnd)
indexStart is the position from where we want to start taking a slice from the original string
if negative index is given, slice() will go from the end of the string, end character will index will be -1, second last character index will be -2 and so on
indexEnd is the position where we want slice() to stop slicing the string
the indexEnd'th will be excluded
if indexStart is getter than indexEnd, then slice() will return ''(empty string)
Examples
conststring="The morning is upon us.";console.log(string.slice(12));// is upon us.console.log(string.slice(-11));// is upon us.console.log(string.slice(13,16));// s uconsole.log(string.slice(16,13));// ''console.log(string.slice(-8,-4));// 'upon'console.log(string.slice(-8,4));// ''console.log(string.slice(8,-4));// 'ing is upon'
5. substring
String.substring() method extracts a section of a string and returns as a new string so they don't change the original string
syntax
String.substring(indexStart, indexEnd)
indexStart is the position from where we want to start taking a slice from the original string
if negative index is given, substring() assumes any negative index as 0, so extract from the start
indexEnd is the position where we want substring() to stop slicing the string
the indexEnd'th will be excluded
if indexStart is getter than indexEnd, then substring() will swap there positions
Examples
conststring="The morning is upon us.";console.log(string.substring(12));// is upon us.console.log(string.substring(-11));// is upon us.console.log(string.substring(13,16));// s uconsole.log(string.substring(16,13));// s uconsole.log(string.substring(-8,-4));// 'upon'console.log(string.substring(-8,4));// 'The'console.log(string.substring(8,-4));// 'The morn'
6. split
String.split(separator) is used whenever we want to divide
our string into substring based on a separator we pass into the split() method
and split() method will return those substring ordered inside an array and returns the array
so it doesn't modify the original array
Examples
conststring="The morning is upon us.";console.log(string.split());// [ 'The morning is upon us.' ]console.log(string.split(" "));// [ 'The', 'morning', 'is', 'upon', 'us.' ]console.log(string.split(""));/* [ 'T', 'h', 'e', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'u', 'p', 'o', 'n', ' ', 'u', 's', '.']*/console.log(string.split("o"));// [ 'The m', 'rning is up', 'n us.' ]
7. includes
String.includes(searchString) is used whenever we want to see or determine
if our string is found within another string, if found returns true else false
Examples
conststring="The brown fox jumps over the lazy dog undefined";console.log(string.includes("fox"));// trueconsole.log(string.includes("Fox"));// falseconsole.log(string.includes());// true
8. startsWith
String.startsWith(searchString) is used whenever we ant to check if a string starts with another string or not
Examples
conststring="Playing cricket is good for health";console.log(string.startsWith("Playing"));// trueconsole.log(string.startsWith("cricket"));// false
9. endsWith
String.endsWith(searchString) checks whether a string ends with another string or not
Examples
conststring="The cat is beautiful!";console.log(string.endsWith("beautiful!"));// trueconsole.log(string.endsWith("beautiful"));// false
10. concat
String.concat(str1, str2, strN) is used to concatenate strings
it returns a new string and doesn't modify original strings
Examples
conststr1="Hello";conststr2="World!";constconcatenatedStr=str1.concat(" ",str2);// Hello World!console.log(concatenatedStr);/* Performance notesit is strongly recommended to use assignment operators (+, +=) instead of the concat() method*/
11. repeat
String.repeat(count) is used whenever we want to do more copies of a string
it takes a count parameter which defines the number copies of the string should be done
returns new string by not modifying original string
if 0 or no parameter is passed repeat() will return empty string
if decimal number is passed, repeat() will convert this into an integer
String.search(regex) is used to search for a match
between regular expression and the string object
search() will always returns an index that is the index of the first match
search() is case sensitive
when search fails it returns -1
Examples
conststring="The rain in SPAIN stays mainly in the plain";console.log(string.search("ain"));// 5 // search() will convert 'ain' into regular expressionconsole.log(string.search(/ain/));// 5console.log(string.search(/AIN/));// 14console.log(string.search(/AIN/i));// 5console.log(string.search(/w/));// -1
19. match
String.match(regex) is used for match
between regular expression and the string object
match() will return an array with some information about the match inside
and it will only return the first match
when match() fails it returns null
Examples
conststring="The rain in SPAIN stays mainly in the plain";console.log(string.match("ain"));// match() will convert 'ain' into regular expression/* [ 'ain', index: 5, input: 'The rain in SPAIN stays mainly in the plain', groups: undefined]*/console.log(string.match(/ain/));console.log(string.match(/AIN/g));// [ 'AIN' ]console.log(string.match(/AIN/gi));// [ 'ain', 'AIN', 'ain', 'ain' ]console.log(string.match(/w/));// null
20. matchAll
String.matchAll(regex) is used for match
between regular expression and the string object
matchAll() will return an array with some information about the match inside
and it will return an iterable of matches containing the results of that search.
when matchAll() fails it returns empty array []
Examples
conststring="The rain in SPAIN stays mainly in the plain";console.log([...string.matchAll("ain")]);// matchAll() will convert this into regular expression like this by adding global modifier// string.matchAll(/ain/g)/* [ [ 'ain', index: 5, input: 'The rain in SPAIN stays mainly in the plain', groups: undefined ], [ 'ain', index: 25, input: 'The rain in SPAIN stays mainly in the plain', groups: undefined ], [ 'ain', index: 40, input: 'The rain in SPAIN stays mainly in the plain', groups: undefined ]]*/console.log([...string.matchAll(/w/gi)]);// []
21. replace
String.replace(searchString, replaceString) is used whenever we want to
replace a substring inside our string with another string
searchString can be string or regular expression
it returns a new string not modifying original string
replace only replace the first occurrence of the match
the second parameter can be a function and the function takes a parameter which is the match
String.replace(searchString, replaceFunction)
Examples
conststring="Mr Blue has a blue house and a blue car";console.log(string.replace("blue","red"));// Mr Blue has a red house and a blue carconstresult=string.replace("blue",(match)=>{returnmatch.toUpperCase();});console.log(result);// Mr Blue has a BLUE house and a blue carconsole.log(string.replace(/blue/g,"red"));// Mr Blue has a red house and a red carconsole.log(string.replace(/blue/gi,"red"));// Mr red has a red house and a red car
22. replaceAll
String.replaceAll(searchString, replaceString) will replace all the match of the string
searchString can be string or regular expression
it returns a new string not modifying original string
the second parameter can be a function and the function takes a parameter which is the match
String.replaceAll(searchString, replaceFunction)
Examples
conststring="Mr Blue has a blue house and a blue car";console.log(string.replaceAll("blue","red"));// Mr Blue has a red house and a red carconstresult=string.replaceAll("blue",(match)=>{returnmatch.toUpperCase();});console.log(result);// Mr Blue has a BLUE house and a BLUE carconsole.log(string.replaceAll(/blue/g,"red"));// Mr Blue has a red house and a red carconsole.log(string.replaceAll(/blue/gi,"red"));// Mr red has a red house and a red car
23. charCodeAt
String.codePointAt(index) is UTF-16 based and range is from 0 to 65535
Returns the Unicode value of the character at the specified location.
when parameter index is not passed by default it would be 0 index
if there is no element at the position(index) it'll return NaN
String.normalize(form) // form => NFC/NFD default NFC
Returns the String value result of normalizing the string into the normalization form named by form as specified in Unicode Standard Annex #15, Unicode Normalization Forms
Examples
// before normalizationletstr1="\u00c7";// Çconsole.log(str1.length);// 1letstr2="\u0043\u0327";// ̧C = C + ̧console.log(str2.length);// 2// after normalizationstr1=str1.normalize();console.log(str1.length);// 1console.log(str1.codePointAt(0));// 199str2=str2.normalize();console.log(str2.length);// 1console.log(str2.codePointAt(0));// 199str1=str1.normalize("NFD");console.log(str1.length);console.log(str1.codePointAt(0));// 67console.log(str1.codePointAt(1));// 807
28. toLowerCase
String.toLowerCase() is used whenever we want to all
the alphabetic character to be lower case or small letter
toLowerCase() returns a new string without changing the original string
Examples
conststring="My name is Jisan";console.log(string.toLowerCase());// my name is jisan
29. toUpperCase
String.toUpperCase() is used whenever we want to all
the alphabetic character to be upper case or capital letter
toUpperCase() returns a new string without changing the original string
Examples
conststring="my name is jisan";console.log(string.toUpperCase());// MY NAME IS JISAN
30. toLocaleLowerCase
String.toLocaleLowerCase(local)
Converts all alphabetic characters to lowercase, taking into account the host environment's current locale.
toLocaleLowerCase() returns a new string without changing the original string