Thursday 24 November 2011

String Function


Ø       big()

·         This function will display the string in a big font.

                    Syntax : stringobject.big()

Ø       small()

·         This function will display the string in a small font.

                        Syntax : stringobject.small()

Ø       bold()

·         This function will display the string in a bold style.

                        Syntax : stringobject.bold()

Ø       italics()

·         This function will display the string in a italic style.

                        Syntax : stringobject.italics()

Ø       strike()

·        This function will strike out the string. Which means the line will be drawn from between the text.

                    Syntax : stringobject.strike()

Example

<html>
          <head>
                   <script language="javascript">
                             var str="Hello World ";
                             document.write(str.big()+"<br/>");
                             document.write(str.small()+"<br/>");
                             document.write(str.bold()+"<br/>");
                             document.write(str.italics()+"<br/>");
                             document.write(str.strike()+"<br/>");                 
</script>
          </head>
     </html>

Ø       fontcolor()

·         The fontcolor() method is used to display a string in a specified color.

          Syntax : stringobject.fontcolor(color)

Parameter

Description

Color

Required. Specifies a font-color for the string. The value can be a color name(red). An RGB value (rgb(255,0,0)) or a hex number (#FFOOOO)

Example

<html>
          <head>
                   <script language="javascript">
                             var s = "Hello World";
                             document.write(s.fontcolor('blue')+"<br/>");
                             document.write(s.fontcolor('rgb(255,0,0)')+"<br/>");
                             document.write(s.fontcolor('#ffoo11'));              
</script>
          </head>
     </html>

Ø       fontsize()

·         The fontsize() method is used to display a string in a specified size.

          Syntax : stringobject.fontsize(size)

Parameter

Description

Size

Required. A number that specifies the font size. Range 1 to 7.

Example

<html>
          <head>
                    <script language="javascript">
                             var s = "Hello World";
                             document.write(s.fontsize(5)+"<br/>");
</script>
          </head>
     </html>

Ø       link()

·         The link() method is used to display a string as a hyperlink.

          Syntax : stringobject.link(stringurl)


Example

<html>
          <head>
                   <script language="javascript">
                             var s = "Click Here";
                             document.write(s.link("str_fontcolor.html")+"<br/>");
</script>
          </head>
     </html>

Ø       charAt()

·         The charAt() method returns the character at a specified position.
·         The first character in the string is at position 0.

            Syntax : stringobject.charAt(index)

Parameter

Description

Index

Required. A number representing a position in the string.

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.charAt(0));
</script>
          </head>
     </html>

Output

          H

Ø       concat()

·         The concat() method is used to join two or more string.

          Syntax : stringobject.concat(string1,string2……….stringn)

Parameter

Description

Stringn

Required. One or more string objects to be joined to a string.

Example


<html>
          <head>
                   <script language="javascript">
                             var str1 = "Hello";
                             var str2 = “world”;
                             document.write(str.concat(str2));
</script>
          </head>
     </html>

Output

Hello World

Ø       indexOf()

·       The indexOf() method returns the position of the first occurrence of a specified string value in a string.
·         The indexOf() method is case sensitive.
·         This method returns -1 if the string value to search for never occurs.

          Syntax : stringobject.indexOf(searchvalue,[fromindex])

Parameter

Description

Searchvalue

Required. Specified a string value to search for

Fromindex

Optional. Specifies where to start the search

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.indexOf("Hello")+"<br/>");
                             document.write(str.indexOf("world"));
</script>
          </head>
     </html>

Output

             0
          -1

Ø       lastIndexOf()

·      The lastIndexOf() method returns the position of the last occurrence of a specified string value in a string.
·         This function is case sensitive.
·         It returns -1 if the search is not found.

          Syntax : stringobject.lastIndexOf(searchvalue,[fromindex])

          Parameter

Description

Searchvalue

Required. Specified a string value to search for

Fromindex

Optional. Startindex parameter accepts the integer value to specify the start position for the lastIndexOf() function.


Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             var str = "Hello World";
                             document.write(str.lastIndexOf("l")+"<br/>");
</script>
          </head>
     </html>

Output

             9

Ø       replace()

·      The replace() method is used to replace some characters with some other character in a string.
·         The replace() method is case sensitive.

          Syntax : stringobject.replace(findstring,newstring)

          Parameter

Description

Findstring

Required. Specified a string value to find. To perform a global search add a ‘g’ flag to this parameter and to perform a case insensitive search add an ‘I’ flag.

Newstring

Required. Specifies the string to replace the found value from findstring.

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str1.replace(/Hello/,"Hi")+"<br/>");
                             document.write(str1.replace(/hello/,"Hi")+"<br/>");
                             document.write(str1.replace(/hello/i,"Hi")+"<br/>");
</script>
          </head>
     </html>

Output

            Hi World
          Hello World
          Hi World

Ø       search()

·         The search() method is used to search a string for a specified value.
·         search() is case sensitive
·      The search() method returns the position of the specified value in the string. If no match was found it returns -1.

            Syntax : stringobject.search(searchstring)

          Parameter

Description

Searchstring

Required. The value to search for in a string. To perform a case insensitive search add an ‘I’ flag.

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.search(/World/)+"<br/>");
document.write(str.search(/world/)+"<br/>");
                             document.write(str.search(/World/i)+"<br/>");
</script>
          </head>
     </html>

Output

            6
          -1
          6

Ø       substr()

·       The substr() method extract a specified number of character in a string, from a start index.
·       To extract character from the end of the end of the string. Use a negative start number.
·         The start index is 0.
·         If the length parameter is omitted, this method extract to the end of the string.

            Syntax : stringobject.substr(start,[length])

          Parameter

Description

Start

Required. Where to start the extraction. Must be a numeric value.

Length

Optional. How many character to extract. Must be a numeric value.

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.substr(3)+"<br/>");
                             document.write(str.substr(3,2)+"<br/>");
</script>
          </head>
     </html>

Output

            lo world
          lo

Ø       substring()

·       The substring() method extract the character in a string between two specified indices.
·         To extract character from the end of the string. Use a negative start number.
·         The start index at 0.
·         If the stop parameter is omitted, this method extracts to the end of the string.

            Syntax : stringobject.substring(start,[stop])

          Parameter

Description

Start

Required. Where to start the extraction. Must be a numeric value.

Length

Optional. Where to stop the extraction. Must be a numeric value.

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.substring(3)+"<br/>");
                             document.write(str.substring(3,7)+"<br/>");
</script>
          </head>
     </html>

Output


            lo World
            lo W

Ø       toLowerCase()

·         The toLowerCase() method is used to display a string in lowercase letters.

            Syntax : stringobject.toLowerCase()

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.toLowerCase());
</script>
          </head>
     </html>

Output


            hello world

Ø       toUpperCase()

·         The toUpperCase() method is used to display a string in uppercase letters.

            Syntax : stringobject.toUpperCase()

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.toUpperCase());
</script>
          </head>
     </html>

Output

            HELLO WORLD

Ø       length()

·         The length property returns the number of character in a string.

            Syntax : stringobject.length

Example

<html>
          <head>
                   <script language="javascript">
                             var str = "Hello World";
                             document.write(str.length);
</script>
          </head>
     </html>

Output

            11

 

Posted By : Hemangi Zala

No comments:

Post a Comment