Skip to content
Related Articles

Related Articles

Improve Article
ES6 | String
  • Last Updated : 11 Oct, 2019

There are two string types string primitives and string objects in ES6 JavaScript. It wraps up the JavaScript’s string primitives by the helper methods. There are a few numbers of helper methods.

  • string primitives: A primitive gets auto-boxing to its wrapper type when a method of the wrapper type is invoked. It has no helper methods, it is nothing more than a pointer to a raw data memory reference. So here it wraps to a string wrapper. (var i = 1) here it wraps to an integer wrapper.
  • string objects: This is a String Object. The String object is created with the help of String Class. It wraps string primitive data type with a number of helper methods.

Syntax: Therer are three properties in ES6 JavaScript mentioined and described below:

var stringobject = new String(string);

String Properties:

PropertiesDescription
ConstructorThis property returns a reference to the string function which created the instance prototype.
LengthThis property returns the length of the string.
PrototypeThis property a global property which is available with almost all the 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.

Below example illuustrates the properties:

Example:






<!DOCTYPE html>
<html>
  
<head>
    <script>
        function hero(id, name) {
            this.id = id;
            this.name = name;
        }
  
        // Ojbect e
        var e = new hero(123, "GeeksforGeeks");
  
        // Ojbect e1
        var e1 = new hero(124, "Courses");
  
        // This prototype property is constant for all objects.
        hero.prototype.group = "Edutech";
  
        // Returns object reference
        document.write("Constructor Property = " + e.constructor);
  
        // Returns length
        document.write("<br>Length of emp.name(GeeksforGeeks) = " + e.name.length);
  
        // Returns the new property(group)
        document.write("<br>Prototype(emp1.name=Courses) group = " + e1.group);
  
        // Returns the new property(group) 
        document.write("<br>Prototype(emp.name=GeeksforGeeks) group = " + e.group);
    </script>
</head>
  
<body>
</body>
  
</html>

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=GeeksforGeeks) group = Edutech

Methods of string Objects: These are the list of methods available in a String object.

MethodsDescription
charAt(character)This methods returns the character at the specified index.
charCodet(character)This methods returns the ascii code of the character at the specified index.
concat(string)This methods returns the concatenated string.
indexOf(string)This methods returns the starting index of the given string.
lastIndexOf(string)This methods returns the starting index of the given string’s last occurence.
localeCompare(string)This methods returns 0, if the compared strings the equal, return 1 or -1 if the compared strings are not equal and 1 and -1 depending on the sorted order.
match(RegExp, string)returns the match of the RegExp.
replace(RegExp, string)returns the replaced RegExp in the string.
search(RegExp)This methods 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).
slice(startIndex, endIndex)This methods returns the string from the startIndex to endIndex(excluding) at the specified index.
substr(startIndex, length)returns the string from the startIndex to startIndex+length(excluding) at the specified index.
substring(startIndex, endIndex)returns the string from the startIndex to endIndex(excluding) at the specified index.
split(“separator”, number of splits)returns an array which got splited with the separator and given b=number of splits, if splits are not mentioned then return the whole array.
toLocalLowerCase()returns a string in lowercase with the current locale.
toLocalUpperCase()returns a string in uppercase with the current locale.
toLowerCase()returns the calling string value converted to lowercase.
toupperCase()returns the character at the specified index.
toString()returns the calling string value converted to uppercase.
valueOf()returns the primitive value of a String object.

Below example will illustrate the methods:
Example:




<!DOCTYPE html>
<html>
  
<head>
    <script>
        var str = new String("A Online Computer Science Portal");
        var str1 = new String("Computer Science Portal for Geeks");
  
        document.write("str.charAt(0) is = " + str.charAt(0));
        document.write("<br>str.charCodeAt(0) is = " + str.charCodeAt(0));
  
        document.write("<br>");
        document.write("<br>str1.concat('=>Geeks?') = " + str1.concat('=>Geeks?'));
  
        document.write("<br>");
        document.write("<br>str.indexOf('of') = " + str.indexOf('of'));
        document.write("<br>str.lastIndexOf('of') = " + str.lastIndexOf('of'));
  
        document.write("<br>");
        document.write("<br>str.localeCompare( 'A Online Computer Science Portal') = " 
                       + str.localeCompare('A Online Computer Science Portal'));
        document.write("<br>str.localeCompare( 'Computer Science Portal for Geeks') = " 
                       + str.localeCompare('Computer Science Portal for Geeks'));
        document.write("<br>str.localeCompare( 'A Computer Science Portal') = " 
                       + str.localeCompare('A Computer Science Portal'));
  
        document.write("<br>");
        if (str.search("Geeksfor") != -1) {
            document.write("<br>Geeks exist in str.");
        } else {
            document.write("<br>Geeks doesn't exist in str.");
        }
  
        document.write("<br>");
        document.write("<br>str.slice(5, -1) = " + str.slice(5, -1));
        document.write("<br>str.substr(2, 9) = " + str.substr(2, 9));
        document.write("<br>str.substring(2, 9) = " + str.substring(2, 9));
  
        document.write("<br>");
        document.write("<br>str.split(' ') = " + JSON.stringify(str.split(' ')));
        document.write("<br>str.split(':', 4) = " + JSON.stringify(str.split(':', 4)));
        document.write("<br>str.split('of', 4) = " + JSON.stringify(str.split('of', 4)));
  
        document.write("<br>")
        document.write("<br>str1.toLocaleLowerCase( ) = " + str1.toLocaleLowerCase());
        document.write("<br>str1.toLocaleUpperCase() = " + str1.toLocaleUpperCase());
        document.write("<br>str1.toLowerCase( ) = " + str1.toLowerCase());
        document.write("<br>str1.toUpperCase() = " + str1.toUpperCase());
  
        document.write("<br>")
        document.write("<br>str1.toString() = " + str1.toString());
        document.write("<br>str1.valueOf( ) = " + str1.valueOf());
    </script>
</head>
  
</html>

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) = ine Computer Science Porta
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



My Personal Notes arrow_drop_up
Recommended Articles
Page :