String Example – Data Type – JavaScript Syntax – JS String Example

String Example – Data Type – JavaScript Syntax – JS String Example

Purpose: – Illustrates the for the .

JavaScript String Examples

String Object Properties
constructor Returns the function that created the String object’s prototype
length Returns the length of a string
prototype Allows you to add properties and methods to an object

String HTML Wrapper Methods
anchor() Creates an anchor
big() Shows the string using a big font
blink() Shows a blinking string
bold() Shows string as bold
fixed() Shows the string using a fixed font
fontcolor() Shows a string in fontcolor
fontsize() Shows a string in fontsize
italics() Shows the string using italics font
link() Shows a string as a link
small() Shows the string using small font
strike() Shows the string using a strikethrough font
sub() Shows a string using subscript font
sup() Shows the string using a superscript font

String Object Methods
charAt() Returns the character at the specified index
charCodeAt() Returns the Unicode of the character at the specified index
concat() returns joined strings
fromCharCode() Converts Unicode values to characters
indexOf() Returns the position of the first found occurrence of a specified value in a string
lastIndexOf() Returns the position of the last found occurrence of a specified value in a string
match() Returns the matchs between a regular expression and a string
replace() Replaces the matched strings with a new strings
search() Returns the position of a string
slice() Returns a part of a string
split() Splits a string into an array of substrings
substr() Returns a portion of the original string
substring() Returns a portion of the original string
toLowerCase() Converts string to lowercase
toUpperCase() Converts string to uppercase
valueOf() Returns the value of the original string

Prerequistes:

Syntax: var myStr = new String(“text”); or var myString = “text”; – Demonstrates how to use properties and methods of String object.

Restrictions: None

Notes:

  • You can build your own library of syntax examples by using same web site over and over and just add new files to it.

Instructions:

  1. Use Visual Web Developer 2008
  2. Create new web site;
    • Click File/New Web Site
    • Select ASP.NET Website Template
    • Select C-Sharp for Language
    • name of Web Site could be JavaScript_Syntax.
  3. Add New folder named “DataType”
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder should be: DataType
  4. Add HTML Page Named String to DataType folder
    • Right-click DataType folder;
    • add new item;
    • Select HTML Page
    • HTML Page name should be String
  5. Click on copy code in code below to copy code into HTML Page String.htm
  6. Right-click on HTML page String and select view in browser
  7. View Example In Browser

    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html>
      <head>
        <title>String</title>
        <script type="text/javascript" >
    //*****************************************************
    // String Object
    //*****************************************************
     
    // NOTE: String Objects are used to store and manipulate
    //       text
    //*****************************************************
     
    // GENERAL SYNTAX:
    //   var myStr = new String("text");
    // Or Simply:
    //    var myString = "text";
     
    //*****************************************************
    //    ***    String Object Properties   ***
    //*****************************************************
                    document.write("***String Object Properties***");
                    document.write("<br />");
    //    Property 	            Description
    //    constructor 	        Returns the function that created the String object's prototype
                    var myStr = "This is a string";
                    document.write("myStr.constructor: " + myStr.constructor); // output: function String() { [native code] }
                    document.write("<br />");
     
    //    length 	        Returns the length of the string
                    document.write("myStr.length: " + myStr.length); // output: 16
                    document.write("<br />");
     
    //    prototype 	        Allows you to add properties and methods to an object
    //    prototype is available for most objects
    //    Syntax: object.prototype.name=value
                    function car(make, model, year) {
                        this.make = make;
                        this.model = model;
                        this.year = year;
                    }
     
                    var myCar = new car("Toyota", "Corolla", 2002);
                    car.prototype.cost = null;
                    myCar.cost = 20000;
     
                    document.write("myCar.cost: " + myCar.cost);
                    document.write("<br />");
     
    //*****************************************************
    //    ***    String HTML Wrapper Methods   ***
    // HTML wrapper methods return the string enclosed in the appropriate
    // HTML tags
    //*****************************************************
                    document.write("***String HTML Wrapper Methods***");
                    document.write("<br />");
     
    //    Method 	        Description
    //    anchor()  	    Creates an anchor
    //    Syntax:           string.anchor(name) - name is required - it is the name of the anchor
                    var strName = "Hyperlink";  // this will be text of hyperlink
                    alert(strName.anchor("Hyper"));  // this will be anchor
     
    //    string.big() 	    Wraps string in <big> tags
                    var strBig = "Big"
                    document.write("strBig.big(): " + strBig.big() + "<br />");
     
    //    string.blink() 	    Wraps string in <blink> tags
                    var strBlink = "blink - only works in Firefox & Opera"
                    document.write("strBlink.blink(): " + strBlink.blink() + "<br />"); // only works
                    // in Firefox & Opera
     
    //    string.bold() 	    Wraps string in <bold> tags
                    var strBold = "bold"
                    document.write("strBold.bold(): " + strBold.bold() + "<br />");
     
    //    string.fixed() 	    Wraps string in <fixed> tags so that string
    //                          displays in fixed pitch font
                    var strFixed = "fixed"
                    document.write("strFixed.fixed(): " + strFixed.fixed() + "<br />");
     
    //    string.fontcolor("color") 	    Wraps string in <fontcolor> tags
    //                                  The value can be a color name (e.g. green),
    //                                  an RGB value (e.g. rgb(0,255,0)), 
    //                                  or a hex number (e.g. #00FF00)
                    var strfontcolor = "fontcolor"
                    document.write("strfontcolor.fontcolor(green): " + strfontcolor.fontcolor("green") + "<br />");
     
    //    string.fontsize(size) 	    Wraps string in <fontsize> tags so that string
    //                          displays in fontsize - size is required and can be from 1-7
                    var strfontsize = "fontsize"
                    document.write("strfontsize.fontsize(7): " + strfontsize.fontsize(7) + "<br />");
     
    //    string.italics() 	    Wraps string in <italics> tags
                    var stritalics = "italics"
                    document.write("stritalics.italics(): " + stritalics.italics() + "<br />");
     
    //    link()  	    Creates an link
    //    Syntax:           string.link(url) - url is required - it is the url of the link
    //                  The method returns a string wrapped in <a> tags: </a><a href="link">string</a>
                    var strLink = "Ideal Programmer";  // this will be text of hyperlink
                    alert(strLink.link("http://idealprogrammer.com"));  // this will be link
     
    //    string.small() 	    Wraps string in <small> tags - displays string in small font
                    var strSmall = "Small"
                    document.write("strSmall.small(): " + strSmall.small() + "<br />");
     
    //    string.strike() 	    Wraps string in <strike> tags - displays string in strkethrough font
                    var strStrike = "Strike"
                    document.write("strStrike.strike(): " + strStrike.strike() + "<br />");
     
    //    string.sub() 	    Wraps string in <sub> tags - displays string in subscript font
                    var strSub = "Subscript"
                    document.write("strSub.sub(): " + strSub.sub() + "<br />");
     
    //    string.sup() 	    Wraps string in <sup> tags - displays string in superscript font
                    var strSuperscript = "Superscript"
                    document.write("strSuperscript.sup(): " + strSuperscript.sup() + "<br />");
     
    //*****************************************************
    //    ***    String Object Methods   ***
    //*****************************************************
     
    //    Method  	Description
    //    charAt(index) 	Returns the character at the specified index - index is required
    //    0 is the first character and the last character in a string is string.length-1
                    var strCharAt = "This is a string.";
                    document.write("strCharAt(0): " + strCharAt.charAt(0) + "<br />");
                    document.write("strCharAt.charAt(strCharAt.length - 1): " + strCharAt.charAt(strCharAt.length - 1));
     
    //    charCodeAt(index) 	Returns the Unicode of the character at the specified index
                    var strcharCodeAt = "This is a string.";
                    document.write("strcharCodeAt(0): " + strcharCodeAt.charCodeAt(0) + "<br />");
                    document.write("strcharCodeAt.charCodeAt(strcharCodeAt.length - 1): " + strcharCodeAt.charCodeAt(strcharCodeAt.length - 1) + "<br />");
     
    //    concat() 	Joins two or more strings, and returns a copy of the joined strings
    //              SYNTAX: string.concat(string2, string3, ..., stringX)
                    var strA = "Aeschylus ";
                    var strB = "was a Greek playwright.<br />";
                    document.write(strA.concat(strB));
     
    //    fromCharCode() 	Converts Unicode values to characters
    //              SYNTAX: String.fromCharCode(n1, n2, ..., nX) - This is a static method
    //              The syntax is String.fromCharCode() and not str.fromCharCode
                    document.write(String.fromCharCode(73, 68, 77, 75, 77));
     
    //    indexOf() 	Returns the position of the first found occurrence of a specified value in a string
    //                  SYNTAX: string.indexOf(searchstring, start) - start is optional
                    var strIndexOf = "Einstein was a philosophical realist";
                    document.write("strIndexOf.indexOf(ein): " + strIndexOf.indexOf("ein") + "<br />");
                    document.write("strIndexOf.indexOf(philo): " + strIndexOf.indexOf("philo") + "<br />");
                    document.write("strIndexOf.indexOf(real): " + strIndexOf.indexOf("real") + "<br />");
     
    //    lastIndexOf() 	Returns the position of the last found occurrence of a specified value in a string
    //                      SYNTAX: string.lastIndexOf(searchstring, start) - start is optional
                    var strlastIndexOf = "Einstein was a philosophical realist";
                    document.write("strlastIndexOf.lastIndexOf(ein): " + strlastIndexOf.lastIndexOf("ein") + "<br />");
                    document.write("strlastIndexOf.lastIndexOf(philo): " + strlastIndexOf.lastIndexOf("philo") + "<br />");
                    document.write("strlastIndexOf.lastIndexOf(l): " + strlastIndexOf.lastIndexOf("l") + "<br />");
     
    //    match() 	Searches for a match between a regular expression and a string, and returns the matches
                    var strMatch = "Einstein defined Morality as giving much and taking very little";
                    var pattern = /ein/gi;
                    document.write("strMatch.match(pattern): " + strMatch.match(pattern) + "<br />");
     
    //    replace() 	Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
    //                  SYNTAX: string.Replace("regexp or oldstring", "newstring") 
                    var strReplace = "Ptolemy thought the earth was flat";
                    document.write("strReplace.replace(Ptolemy, The Ancients)" + strReplace.replace("Ptolemy", "The Ancients") + "<br />");
     
    //    search() 	Searches for a match between a regular expression and a string, and returns the position of the match
    //                  SYNTAX: string.search("regexp or string") 
                    var strSearch = "Copernicus said the sun is in the center.";
                    document.write("strSearch.search(sun): " + strSearch.search("sun") + "<br />");
     
    //    slice() 	Extracts a part of a string and returns a new string
    //              SYNTAX: string.slice(beginIndex,endIndex) - endIndex is optional
                    var strSlice = "Copernicus said the sun is in the center.";
                    document.write("strSlice.slice(0): " + strSlice.slice(0) + "<br />");
     
                    var strSliceA = "Copernicus said the sun is in the center.";
                    document.write("strSliceA.slice(7,20): " + strSliceA.slice(7,20) + "<br />");
     
     
    //    split() 	Splits a string into an array of substrings
    //              SYNTAX: string.split(separator,limit) 
    //              separator is the character(s) that define where split occurs
    //              limit is optional and defines max number of strings to return
                    var strSplit = "Copernicus said the sun is in the center.";
                    document.write("strSplit.split( ): " + strSplit.split(" ") + "<br />");
     
                    var strSplitA = "Copernicus said the sun is in the center.";
                    document.write("strSplitA.split( ,3): " + strSplitA.split(" ",3) + "<br />");
     
    //    substr() 	Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
    //              SYNTAX: string.substr(startIndex,EndIndex) - EndIndex is optional
                    var strSubstr = "Newton believed in action at a distance.";
                    document.write("strSubstr.substr(0,15): " + strSubstr.substr(0, 15) + "<br />");
     
     
    //    substring() 	Extracts the characters from a string, between two specified indices
    //              SYNTAX: string.substr(fromIndex,ToIndex) - toIndex is optional and is not included
    //              in the returned string
                    var strSubstring = "Newton believed in action at a distance.";
                    document.write("strSubstring.substring(0,15): " + strSubstring.substring(0, 15) + "<br />");
     
    //    toLowerCase() 	Converts a string to lowercase letters
                    var strToLowerCase = "Einstein believed space and time are not rigid and linear.";
                    document.write("strToLowerCase.toLowerCase(): " + strToLowerCase.toLowerCase() + "<br />");
     
    //    toUpperCase() 	Converts a string to Uppercase letters
                    var strToUpperCase = "Einstein believed space and time are not rigid and linear.";
                    document.write("strToUpperCase.toUpperCase(): " + strToLowerCase.toUpperCase() + "<br />");
     
     
    //    valueOf() 	Returns the primitive value of a String object
                    var strValueOf = "Einstein believed space and time are not rigid and linear.";
                    document.write("strValueOf.valueOf(): " + strValueOf.valueOf() + "<br />");
     
     
     
        </sup></sub></strike></small></italics></fontsize></fontcolor></fixed></bold></blink></big></script>
      </head>
      <body>
      </body>
    </html>