Broad Network


String Nature Methods of ECMAScript 2015 String Prototype

The ECMAScript String Object – Part 2

ECMAScript 6

Foreword: In this part of the series I talk about String Nature Methods of ECMAScript String Prototype.

By: Chrysanthus Date Published: 14 Jul 2016

Introduction

This is part 2 of my series, The ECMAScript String Object. In this part of the series I talk about String Nature Methods of ECMAScript String Prototype. A prototype is an object, which is a property of the constructor. This object is responsible for inheritance (creating objects of a particular type). To use its properties, you do not type the prototype directly, you type but the created object. You should have read the previous part of the series before reaching here, as this is a continuation.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Obtaining the String Literal
There are two prototype methods to return the string literal. The syntax is:

    stringObject.toString ( )

or

    stringObject.valueOf ( )

Read and try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

    <script type="text/ECMAScript">

        strObj = new String("Where are the girls?");

        strLA = stringObject.toString();
        strLB = stringObject.valueOf();

        document.write(strLA);document.write('<br>');
        document.write(strLB);

    </script>

</body>
</html>

The extra HTML code is to produce a web page. The Output is:

Where are the girls?
Where are the girls?

The string literal is like a property of the string object, so there has to be a way of reading it. The toString() and valueOf() methods offer that way.

Knowing the character at a Position in the String
Index counting begins from zero. The leftmost character at position 1 in the string literal is at index 0, the second character is at index 1, the third is at index 2, and so on. The syntax to determine the character at a position is:

    stringObject.charAt (index)

This returns the character.

Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String("The birds are here.");

        char = strObj.charAt(4);
        alert(char);

    </script>

The output is b of the word, “bird” at index 4, which is position 5 (always subtract 1 from position to have index.).

Character Code
Each character in a string has a code unit value (number). The code unit value of a character at any position in the string can be returned. The syntax for this is:

    stringObject.charCodeAt (index)

The code for b in the word “bird” is returned, in the following script:

    <script type="text/ECMAScript">

        strObj = new String("The birds are here.");

        code = strObj.charCodeAt(4);
        alert(code);

    </script>

The index of the character is 4 for b and the output is:

    98

Changing Case from Lower to Upper
You can change all the characters in a string from lowercase to uppercase; if any character was already in uppercase, it remains in uppercase. The syntax to do this is:

    stringObject.toUpperCase()

Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String("This is the woman.");

        str = strObj.toUpperCase();
        document.write(str);

    </script>

The Output is :

    THIS IS THE WOMAN.

Changing Case from Upper to Lower
You can change all the characters in a string from uppercase to lowercase; if any character was already in lowercase, it remains in lowercase. The syntax to do this is:

    stringObject.toLowerCase ( )

Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String("UNESCO is of the UNO.");

        str = strObj.toLowerCase();
        document.write(str);

    </script>

The output is:

    unesco is of the uno.

Trimming
You can remove leading (appearing before) and trailing (appearing after) white space from text of a string. The syntax to do this is:

    stringObject trim ( )

Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String(" tLet us have a word.n ");

        str = strObj.trim();
        document.write(str);

    </script>

Substring
You can read out a portion of string from the main string. The syntax is:

    stringObject.substring (start, end)

The substring is read from the character corresponding to the index, start, up to but not including the character corresponding to the index, end. So, to read the whole string from index 0, the end index has to be the length of the string (last index + 1). The original string remains unchanged. If the second argument is not given, the substring is taken right to the end of the main string, from the start position (whatever it is). Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String("We are the world. We are the children. We are the ones.");

        str = strObj.substring(18, 37);
        document.write(str);

    </script>

The output is:

    We are the children

Slicing
Slicing is the same as reading the substring. The syntax is:

    stringObject.slice(start, end)

Read and try the following code:

    <script type="text/ECMAScript">

        strObj = new String("We are the world. We are the children. We are the ones.");

        str = strObj.slice(18, 37);
        document.write(str);

    </script>

With the same start and end indices, the output is the same as for the previous code, as follows

    We are the children

That is it, for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message