String Objects in JavaScript and SubString Objects Properties and Methods
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<html> <body> <script type="text/javaScript"> var text = "Programming Digest"; document.write(text.link("https://programmingdigest.com")); </script> </body> </html> |
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”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<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 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”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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 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”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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 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”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<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 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<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> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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> |
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():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html> <head> <script type="text/javaScript"> var text = "PROGRAMMING DIGEST"; document.write(text+" = "+text.toLowerCase()); </script> </head> <body> </body> </html> |
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():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html> <head> <script type="text/javaScript"> var text = "programming digest"; document.write(text+" = "+text.toUpperCase()); </script> </head> <body> </body> </html> |