String Objects in JavaScript and SubString Objects Properties and Methods

string objects

String objects in JavaScript:

string objects are commonly used to process stings. This JavaScript object also has properties and methods

Properties of String objects:

The most commonly used properties of this object is the “length” property

String Objects Length:

This property returns the number of characters in a string. The general syntax to use this property is:

StringObject.length

Methods of String objects:

The string objects have several methods. The important and commonly used methods are as follows:

String Objects Achor():

This method is used t create an Html anchor. The general syntax to use this method is:

Stringobjects.anchor()

The following statement create an anchor:

var txt = “testing”

document.write(txt.anchor(“myanchor”));

the above statement are equivalent to:

<a name=”myanchor”> testing </a>

String Objects Big():

This method is used to display a string in a big font. The general syntax to use this method is:

Stringobject.big()

String Objects small():

This method is used to display a string in a small font. The general syntax to use this method is:

Stringobject.small()

Blink():

This method is used to display a string as blinking. The general syntax to use this method is:

Stringobject.blink()

String Objects Bold():

This method is used to display a string in bold style. The general syntax to use this method is:

Stringobject.bold()

String Objects Italics():

This method is used to display a string in italic style. The general to use this method is:

Stringobject.italics()



String Objects charAt():

this method returns a character at a specified position of a string. The general syntax to use this method is:

stringobject.charAt(x)

where ‘x’ specifies the position of character in a string. Please note that the first character in the string is at position 0.

String Object Concat():

This method is used to join two or more string. The general syntax to use this method is:

Stringobject.concat(string1, string2,…)

The following statement join two string

var str = “programming”;

var str1= “ digest”;

document.write(str.concat(str1));

output will be:

programming digest

String Object fontcolor():

this method is used to display a string in a specified color. The general syntax to use this method is:

stringobject.fontcolor(color)

where color specifies the font color for the string. The value can be color name (such as green, blue, red etc.), an RGB value(rgb(255,0,0)), a hexadecimal value (#ff0000).

String Objects Fontsize():

This method is used to display a string in a specified size. The general syntax to use this method is:

Stringobject.size(value)

Where value specifies the font size for string. Its value may be from 1 to 7.

String Objects indexOf():

this method returns the position of the first occurrence of a specified substring. The general syntax to use this method is:

stringobject.indexOf(substr, start)

where substr specifies the substring to search for and start specifies where to start the search. If search is unsuccessful, value -1 is returned by this method.

String Objects lastindexOf():

this method returns the position of the last occurrence of a specified substring in a string. The searching starts from the specified position to backward. The general syntax to use this method is:

stringobject.lastindexOf(substr, start)

where substr specifies the substring to search for and start specifies where to start the search. If search is unsuccessful, value -1 is returned by this method.

String Objects Link():

This method is used to display a string as hyperlink. The general syntax to use this method is:

Stringobject.link(url)

Where url specifies the url of the page. The following statement display programmingdigest hyperlink:

 

JavaScript Objects


String Objects Match():

This method is used to search a specified substring in a string. This method is similar t inexOf() and lastindexOf() methods, but it returns the specified value, instead of the position of the string. It returns null if the specified substring not found in a string. The general syntax to use this method is:

Stringobject.match(substr)

Where “substr” specifies the substring to search in a string please note that match() method is case-sensitive.

The following statements do different searches within a string “programming digest”:

 

JavaScript Objects

String Objects Search():

This method is also used to search a specified substring in a string. This method returns the string position of the substring in the string. It returns -1 if the specified substring not found in the string. The general syntax to use this method is:

Stringobject.search(substr)

Where “substr” specifies the substring to search in a string this method is also case-sensitive.

The following statement do different search within a string “Programming digest”:

 

JavaScript Objects

String Objects Replace():

This method is used to search a specified substring in a string and replace with some other one. The general syntax to use this method is:

Stringobject.replace(findstr, newstr)

Where “findstr” specifies the substring to search in a string and “newstr” specifies the string to replace with the found string.

The following statement replaces “Programming Digest” with “programming digest”:

 

JavaScript Objects


String Objects Slice():

This method extracts and returns a part of string. The general syntax to use this method is:

Stringobject.slice(start, end)

Where start specifies the string position of the part of string to extract and end specifies the last position of part of string. Its use is optional. If it is omitted, the part of string is extracted up to the end of string.

The following statements extract different parts of a string “Programming Digest”:

 

JavaScript Objects

String Objects Substring():

This method is also used to extract characters in a string between two indices. The general syntax to use this method is:

Stringobject.substring(start, end)

String Objects Sub():

This method is used to display s string s subscript. The general syntax to use this method is:

Stringobject.sub()

The following code demonstrates the function of sub:

 

JavaScript Objects

Sup():

This method is used to display a string as superscript. The general syntax to use this method is:

Stringobject.sup()

The following code demonstrate the function of sup:

 

JavaScript Objects



toLowerCase():

this method is used to convert a string into lowercase letters. The general syntax to use this method is:

stringobject.toLowerCase()

The following code demonstrates the function of toLowerCase():

 

JavaScript Objects

toUpperCase():

this method is used to convert a string into Uppercase letters. The general syntax to use this method is:

stringobject.toUpperCase()

The following code demonstrates the function of toUpperCase():

 

JavaScript Objects

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: