JavaScript Strings Operations and Methods            As yo

JavaScript Strings Operations and Methods            As you should, strings are a kind of variable
Generic placeholder image

Mukesh R

JavaScript Strings Operations and Methods

           As you should, strings are a kind of variables which contain a text. The text can have any length from zero to a complete page or more; and it can also contain HTML tags or other languages codes. It is not important what you write in the double or single quotes, they are all characters with no extra worth. But when you call them in the functions the worth appears (like what we had in the previous example lesson about using HTML tags as an input data for 'document.write ()' function).
 
Strings are infact more than a single variable; they act as objects too, and have some properties and methods. In this lesson we want to illustrate them. Let’s start with simple example that gets a string and shows its length (the number of its characters):
 
In Notepad We Write
<script>
   var str= "Hello I am a new JavaScript Programmer"
   document.write ( str.length)
</script>
 
You must see 38 as the result. The procedure is simple; first we define a string variable and then we used its name to get access to its properties and methods. 'length' is a property which returns this variable length. You can also save the result in another string variable instead of printing it on the page. Note that there is no difference between defining a string variables and other kinds before the equal sign; the quotes themselves do it.
 
Other methods are listed in the following table:
 
Method
Description
.toLowerCase ()
Returns a text with all characters in small letters
.toUpperCase ()
Returns a text with all characters in CAPITAL letters
.bold ()
Returns a text in bold format
.italics ()
Returns a text in italic format
.fontsize (num)
Returns a text in font size num (1 to 7)
.fontcolor ("color")
Returns a text in specified color
.big ()
Returns a text in one size bigger
.substring (n1,n2)
Returns a text starting from the character number  n1 to character number n2 (starts counting from first character with 0 index, for both) 
.substr (a,b)
Returns a text from the character number  n1 to character number n2 (starts counting from first character with 0 index, for n1, and from n1 for n2) 
.replace( "text1","text2")
Returns a text with replacement of first "text1" statement to "text2" statement.
.indexOf ("text",[num])
Returns the number of first character of "text" after optional numth character (first character has zero index and returns -1 if not found)
.lastIndexOf ("text",[num])
Returns the number of first character of "text" statement before optional numth character (first character has zero index and returns -1 if not found)