JavaScript

String JavaScript Object with Examples

String JavaScript object:

The string object is commonly used to process stings. This JavaScript object also has properties and methods



Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Properties of String JavaScript object:

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

Length:

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

StringObject.length

Methods of String JavaScript object:

The string object has several methods. The important and commonly used methods are as follows:

Achor():

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

Stringobject.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>

Big():

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

Stringobject.big()


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()

Bold():

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

Stringobject.bold()

Italics():

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

Stringobject.italics()

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.

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

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).


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.

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.

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.

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:

<html>
<body>
<script type="text/javaScript">
var text = "Programming Digest";
    document.write(text.link("https://www.programmingdigest.com"));

</script>
</body>
</html>

string javascript object

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”:

<html>
<head>

<script type="text/javaScript">
var text = "Programming Digest";
document.write(text.match("pro") + "<br>");
document.write(text.match("Programming") + "<br>");
document.write(text.match("di") + "<br>");
document.write(text.match("Digest") + "<br>");

</script>

</head>
<body>

</body>
</html>

 

string javascript object


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”:

<html>
<head>

<script type="text/javaScript">
var text = "Programming Digest";
document.write(text.search("Digest") + "<br>");
document.write(text.search("Programming") + "<br>");



</script>

</head>
<body>

</body>
</html>

string javascript object

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 replace “Programming Digest” with “programming digest”:

<html>
<head>

<script type="text/javaScript">
var text = "Programming Digest";
document.write(text+ " = "+ text.replace(text, "programming digest") + "<br>");

</script>

</head>
<body>

</body>
</html>

 

string javascript object



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 extracts different parts of a string “Programming Digest”:

<html>
<head>

<script type="text/javaScript">
var text = "Programming Digest";
document.write(text.slice(0,4)+"<br>");
document.write(text.slice(4,11)+"<br>");
document.write(text.slice(12,15)+"<br>");
document.write(text.slice(15,18)+"<br>");

</script>

</head>
<body>

</body>
</html>

 

string javascript object

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)

Sub():

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

Stringobject.sub()

The following code demonstrate the function of sub:

<html>
<head>

<script type="text/javaScript">
var text = "2";
document.write("X"+text.sub()+"<br>");
document.write("H"+"2".sub()+"O"+"<br>");


</script>

</head>
<body>

</body>
</html>

string javascript object


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:

<html>
<head>

<script type="text/javaScript">
var text = "2";
document.write("X"+text.sup()+"<br>");
document.write("mc"+"2".sup()+"<br>");


</script>

</head>
<body>

</body>
</html>

string javascript object

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():

<html>
<head>

<script type="text/javaScript">
var text = "PROGRAMMING DIGEST";
document.write(text+" = "+text.toLowerCase());

</script>

</head>
<body>

</body>
</html>

string javascript object


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():

<html>
<head>

<script type="text/javaScript">
var text = "programming digest";
document.write(text+" = "+text.toUpperCase());

</script>

</head>
<body>

</body>
</html>

string javascript object

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button