Use OF String Object In Javascript

Hey, there we bring another awesome tutorial on string object, as we have discussed other properties in this tutorial we will have two major parts which are String Properties and String Methods. A string object allows one to work with a series of characters. You can call any of the helper methods of the string object on string primitive as the Javascript automatically converts between string primitives and objects, We may have this syntax to illustrate the string object.
    
        var val=new String(string);
Whereby string parameter represents series of characters.

1. String Properties

This marks our first part of our tutorial today, we discuss various types of String Properties which may include; constructor, length, prototype. We will discuss each giving the relevant examples for better understanding;
a) constructor-It returns the string function that was used to create the instances prototype, we may have its syntax as;
     
       string.constructor  
     
 
For better understanding we may have the following example;
<head>
<title>Example for string constructor property</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "The string is" );
document.write("str.constructor is:" + str.constructor);
</script>
</body>

b) Prototype

As we discussed in other objects this type of property allows one to add methods and properties to any object such as Boolean, number among others. We may have its syntax as;

object.prototype.name=value

For better understanding we may an example as below;

<head>
<title>Example for prototype string property</title>
<script type="text/javascript">
function phone(type, android){
this.type = type;
this.android = android;
}
</script>
</head>
<body>
<script type="text/javascript">
var myPhone = new phone("Tecno", "10");
phone.prototype.price = null;
myPhone.price = 10000;
document.write("Phone type is : " + myPhone.type + "<br>");
document.write("Phone android is : " + myPhone.android + "<br>");
document.write("Phone price is : " + myPhone.price + "<br>");
</script>
</body>

It will produce;
Phone type is: Tecno
Phone android is: 10
Phone price is: 10000

c) Length

It returns the number of characters in a string. We may it’s syntax as;

string.length

For better understanding let’s have an example here;

<script type="text/javascript">
var str = new String( "The string is" );
document.write("str.length is:" + str.length);
</script>

It will produce;
str.length is: 13

2. STRING METHODS

This marks our second and very important part of our tutorial, here we will discuss various types of string methods as we have discussing in other objects, the string methods here may include; concat(), indexOf(), lastIndexOf(),match(), replace(), search(), split(), toString(), substr(), toLowerCase(), valueOf(), toUpperCase(). You will find that this methods are the same with methods we discussed in other objects. Let’s now discuss some of this methods giving relevant examples;

  • lastIndexOf()

It returns the index within the calling string object of the last occurrence of the specified value, otherwise it returns -1 if not found. we may have it’s syntax as;

string.lastIndexOf(searchValue[, fromIndex])

Where the searchvalue represents the value to search for while fromIndex represents the location where to start the search.

For better understanding let’s have an example here;

<script type="text/javascript">
var str1 = new String( "This is string one and again string" );
var index = str1.lastIndexOf( "string" );
document.write("lastIndexOf found String is:" + index );
document.write("<br />");
var index = str1.lastIndexOf( "one" );
document.write("lastIndexOf found String is:" + index );
</script>

It will produce;
lastIndexOf found String is: 29
lastIndexOf found String is: 15
  • Search()

It returns the search for a match between a regular expression and this string object, otherwise it returns -1. We may have it’s syntax as below;

string.search(regexp);

We may have this example for better understanding;

<script type="text/javascript">
var re = /mangoes/gi;
var str = "mangoes are round, and mangoes are juicy.";
if ( str.search(re) == -1 ){
document.write("Does not contain mangoes" );
}else{
document.write("Contains mangoes" );
}
</script>

It will produce;
contains mangoe
  • toLowerCase()

Here the calling string value is converted to lowercase. We may have the syntax as;

string.toLowerCase()

We may have an example here to see the result;

<script type="text/javascript">
var str = "Mangoes Are Sweet, and Mangoes Are Cheap.";
document.write(str.toLowerCase( ));
</script>

It will produce;
mangoes are sweet, and mangoes are cheap

NB. This method is similar to toUpperCase(), where the calling string value is converted to uppercase.

NB. Most of the methods we have covered them in other objects remember to visit our previous tutorials to read about them, with the similar examples and try them, they are all the same. Let’s now look at string HTML wrappers, which may include the following;

  • big()– It makes the string to be displayed in big font. The syntax is; string.big(), here is an example for the same
<script type="text/javascript">
var str = new String("Hello world");
alert(str.big());
</script>

the string hello world will appear in big font
  • small() – the string appears in small font. The syntax is; string.small()
  • fixed() – the string is displayed in fixed-pitch font. The syntax is; string.fixed()
  • fontcolor()– the string is displayed in the specified color. The syntax is; string.fontcolor(red)
  • fontsize() -the string is displayed in the specified fort size. The syntax is; string.fontsize(12)
  • italics() – it returns the string in italics. The syntax is; string.italics()
  • bold() – it returns the string in bold type. The syntax is; string.bold()
  • sub() – the string appears in subscript. The syntax is; string.sub()
  • sup() – the string is displayed in superscript. The syntax is; string.sup()
  • blink() -it makes the string to blink. The syntax is; string.blink()
  • anchor()– it creates the Html anchor that is used as a hypertext target. The syntax is; string.anchor(anchorname)

NB. Remember in the above illustrated methods you can try them using the example given for big() above, they are similar you just with any of the above and see the result.

With that we have covered all types of objects in Javascript, therefore next in our tutorial we discuss now the advanced parts of Javascript such as form validations and Animations. For live sessions and other presentations subscribe to our YouTube channel here;YOUTUBE LINK. Thanks for reading through our tutorial, As we continue learning Javascript, hoping to meet you in our next tutorial.