Methods of String Objects
These are the list of methods available in a String object.
Methods | Description |
---|---|
charAt(character) |
This method returns the character at the specified index. character = str.charAt(index) |
charCodeAt(character) |
This method returns the ascii code of the character at the specified index. str.charCodeAt(index) |
concat(string) |
This method returns the concatenated string. str.concat(string2, string3, string4,......, stringN) |
indexOf(string) |
This method returns the starting index of the given string. str.indexOf(searchValue , index) |
lastIndexOf(string) |
This method returns the starting index of the given string’s last occurrence. str.lastIndexOf(searchValue , index) |
localeCompare(string) |
This method returns 0, if the compared strings the equal, returns 1 or -1 if the compared strings are not equal, and 1 and -1 depending on the sorted order. referenceString.localeCompare(compareString) |
match(RegExp, string) |
returns the match of the RegExp. string.match(regExp) |
replace(RegExp, string) |
returns the replaced RegExp in the string. str.replace(value1, value2) |
search(RegExp) |
This method searches for the given word or regex. If a string is passed as a parameter it gets converted into RegExp by using new RegExp(obj). string.search( A ) |
slice(startIndex, endIndex) |
This method returns the string from the startIndex to the endIndex(excluding) at the specified index. string.slice(startingIndex, endingIndex) |
substr(startIndex, length) |
returns the string from the startIndex to startIndex+length(excluding) at the specified index. str.substr(start , length) |
substring(startIndex, endIndex) |
returns the string from the startIndex to endIndex(excluding) at the specified index. string.substring(Startindex, Endindex) |
split(“separator”, number of splits) |
returns an array that got split with the separator and given b=number of splits, if splits are not mentioned then return the whole array. str.split(separator, limit) |
toLocaleLowerCase() |
returns a string in lowercase with the current locale. str.toLocaleLowerCase() |
toLocaleUpperCase() |
returns a string in uppercase with the current locale. str.toLocaleUpperCase() |
toLowerCase() |
returns the calling string value converted to lowercase. str.toLowerCase() |
toupperCase() |
returns the character at the specified index. str.toUpperCase() |
toString() |
returns the calling string value converted to uppercase. string.toString() |
valueOf() |
returns the primitive value of a String object. string.valueOf() |
Example: In this example, we will show the use of ES6 String methods usage.
JavaScript
let str = new String( "A Online Computer Science Portal" ); let str1 = new String( "Computer Science Portal for Geeks" ); console.log( "str.charAt(0) is = " + str.charAt(0)); console.log( "str.charCodeAt(0) is = " + str.charCodeAt(0)); console.log( "str1.concat('=>Geeks?') = " + str1.concat( '=>Geeks?' )); console.log( "str.indexOf('of') = " + str.indexOf( 'of' )); console.log( "str.lastIndexOf('of') = " + str.lastIndexOf( 'of' )); console.log( "str.localeCompare( 'A Online Computer Science Portal') = " + str.localeCompare( 'A Online Computer Science Portal' )); console.log( "str.localeCompare( 'Computer Science Portal for Geeks') = " + str.localeCompare( 'Computer Science Portal for Geeks' )); console.log( "str.localeCompare( 'A Computer Science Portal') = " + str.localeCompare( 'A Computer Science Portal' )); if (str.search( "Geeksfor" ) != -1) { console.log( "Geeks exist in str." ); } else { console.log( "Geeks doesn't exist in str." ); } console.log( "str.slice(5, -1) = " + str.slice(5, -1)); console.log( "str.substr(2, 9) = " + str.substr(2, 9)); console.log( "str.substring(2, 9) = " + str.substring(2, 9)); console.log( "str.split(' ') = " + JSON.stringify(str.split(' '))); console.log("str.split(' : ', 4) = " + JSON.stringify(str.split(' : ', 4))); console.log("str.split(' of ', 4) = " + JSON.stringify(str.split(' of', 4))); console.log( "" ) console.log( "str1.toLocaleLowerCase( ) = " + str1.toLocaleLowerCase()); console.log( "str1.toLocaleUpperCase() = " + str1.toLocaleUpperCase()); console.log( "str1.toLowerCase( ) = " + str1.toLowerCase()); console.log( "str1.toUpperCase() = " + str1.toUpperCase()); console.log( "" ) console.log( "str1.toString() = " + str1.toString()); console.log( "str1.valueOf( ) = " + str1.valueOf()); |
Output:
str.charAt(0) is = A
str.charCodeAt(0) is = 65
str1.concat('=>Geeks?') = Computer Science Portal for Geeks=>Geeks?
str.indexOf('of') = -1
str.lastIndexOf('of') = -1
str.localeCompare( 'A Online Computer Science Portal') = 0
str.localeCompare( 'Computer Science Portal for Geeks') = -1
str.localeCompare( 'A Computer Science Portal') = 1
Geeks doesn't exist in str.
str.slice(5, -1) = online Computer Science Portal
str.substr(2, 9) = Online Co
str.substring(2, 9) = Online
str.split(' ') = ["A","Online","Computer","Science","Portal"]
str.split(':', 4) = ["A Online Computer Science Portal"]
str.split('of', 4) = ["A Online Computer Science Portal"]
str1.toLocaleLowerCase( ) = computer science portal for geeks
str1.toLocaleUpperCase() = COMPUTER SCIENCE PORTAL FOR GEEKS
str1.toLowerCase( ) = computer science portal for geeks
str1.toUpperCase() = COMPUTER SCIENCE PORTAL FOR GEEKS
str1.toString() = Computer Science Portal for Geeks
str1.valueOf( ) = Computer Science Portal for Geeks
We have a complete list of Javascript ES6 features, to check those please go through this Introduction to ES6 article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic Guide to JavaScript.
ES6 String
ES6 string is an object that is used to represent a sequence of characters or manipulate the character’s sequence and It can contain zero or more characters within quotes. The text within the single or double quotes is called a string in Javascript.