Powered by Blogger.

Wednesday, February 19, 2014

Format Strings in javascript

Here in this article, we will discuss about how to format Strings in javascript. Also you can check:

- Jagged arrays in C#.Net

- Export gridview data to excel sheet in Asp.net

- How to implement FileUpload Control in Update Panel using asp.net?

When you create a text string in JavaScript, a string object is associated with that string. The string object provides a series of methods you can use to adjust the format of the string. This can be useful when you want to display a string and quickly apply some formatting to it.

The methods are as follows:

- big: Returns the string in big tags

- blink: Returns the string in blink tags

- bold: Returns the string in b tags

- fixed: Returns the string in tt tags (for fixed-width display)

- fontcolor: Returns the string in font tags with the color attribute set to the color you specify as an argument

- fontsize: Returns the string in font tags with the size attribute set to the size you specify as an argument

- italics: Returns the string in i tags

- small: Returns the string in small tags

- strike: Returns the string in strike tags (for a strikethrough effect)

- sub: Returns the string in sub tags (for a subscript effect)

- sup: Returns the string in sup tags (for a superscript effect)

- toLowerCase: Returns the string with all lowercase characters

- toUpperCase: Returns the string with all upper case characters

The following task displays the same string using each of these methods:

1. Open a new HTML document in your preferred HTML or text editor.
2. Create the body of the document with opening and closing body tags:


3. Insert a script block in the body of the document:
<script language="JavaScript">
// -->

4. Create a variable named myVariable and assign the value "Hello there" to it:

var myVariable = "Hello there";

5. Use the document.write method to display the value of the variable
as altered by each of the formatting methods

<script language="JavaScript">
    var myVariable = "Hello there";
    document.write(myVariable.big() + "<br>");
    document.write(myVariable.blink() + "<br>");
    document.write(myVariable.bold() + "<br>");
    document.write(myVariable.fixed() + "<br>");
    document.write(myVariable.fontcolor("red") + "<br>");
    document.write(myVariable.fontsize("18pt") + "<br>");
    document.write(myVariable.italics() + "<br>");
    document.write(myVariable.small() + "<br>");
    document.write(myVariable.strike() + "<br>");
    document.write(myVariable.sub() + "<br>");
    document.write(myVariable.sup() + "<br>");
    document.write(myVariable.toLowerCase() + "<br>");
    document.write(myVariable.toUpperCase() + "<br>");
// -->

6. Open the file in a browser. You should see the text "Hello there" displayed once for each of the formatting methods.


Post a Comment