Open In App

ES6 String

Last Updated : 19 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Methods to Create a String in ES6 JavaScript:

There are two ways to create a string in ES6 JavaScript:

  • Using a string literal
  • Using String Object

Method 1: Using a String literal

Syntax:

let str = "GeeksforGeeks";

Example:

Javascript




let str = "Geeks"
console.log(str);
 
let str1 = "JavaScipt"
console.log(str1);


Output

Geeks
JavaScipt


Method 2: By using String Object

This method takes in arguments and creates a new String object.

Syntax:

let stringobject = new String('literal');

Example:

Javascript




let n = 3457554
let str = new String(n)
console.log(str)
 
let str1 = new String('JavaScript');
console.log(str1);


Output

[String: '3457554']
[String: 'JavaScript']


String Properties:

A static property is a property that has the same value for the entire class.

Properties Description
Constructor This property returns a reference to the string function which created the instance prototype.
Length This property returns the length of the string.
Prototype This property is a global property that is available with almost all objects. This allows you to add properties and methods to an object, where the prototyped object has different behavior compared to string primitive auto-boxing.

Example: In this example, we will show the use of ES6 String properties usage.

Javascript




function hero(id, name) {
    this.id = id;
    this.name = name;
}
 
// Object e
let e = new hero(123, "GeeksforGeeks");
 
// Object e1
let e1 = new hero(124, "Courses");
 
// This prototype property is constant for all objects.
hero.prototype.group = "Edutech";
 
// Returns object reference
console.log("Constructor Property = " + e.constructor);
 
// Returns length
console.log("Length of emp.name(GeeksforGeeks) = " + e.name.length);
 
// Returns the new property(group)
console.log("Prototype(emp1.name=Courses) group = " + e1.group);
 
// Returns the new property(group)
console.log("Prototype(emp.name=GeeksforGeeks) group = " + e.group);


Output

Constructor Property = function hero(id, name) {
    this.id = id;
    this.name = name;
}
Length of emp.name(GeeksforGeeks) = 13
Prototype(emp1.name=Courses) group = Edutech
Prototype(emp.name=Geeksf...

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()
str.toLocaleLowerCase(locale)
toLocaleUpperCase()

returns a string in uppercase with the current locale.

str.toLocaleUpperCase()
str.toLocaleUpperCase(locale)
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.  



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads