{"id":344,"date":"2011-12-20T10:06:35","date_gmt":"2011-12-20T10:06:35","guid":{"rendered":"http:\/\/javascript-tutor.net\/index.php\/"},"modified":"2017-11-18T05:43:55","modified_gmt":"2017-11-18T05:43:55","slug":"lesson-11-objects-part-3-string-object","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/lesson-11-objects-part-3-string-object\/","title":{"rendered":"JavaScript Lesson 11: The String Object"},"content":{"rendered":"<h4 align=\"center\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-10-objects-part-2-math-object\/\">&lt;Lesson 10&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-12-objects-part-4-document-object\/\">&lt;Lesson 12&gt;<\/a><\/strong><\/h4>\n<p>The String object in JavaScript is for manipulating texts and characters.\u00a0 It can be used to process names, addresses, phone numbers, ID, company names, serial numbers and so on. As a matter of facts,\u00a0 JavaScript statements comprise strings and characters. A string is made up of letters, numbers and alphanumeric characters such as #,@,$,!,&amp;,*,\\,+,- and so on. The string object operates using various methods, as follows:<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3><strong>11.1 String Methods<\/strong><\/h3>\n<p>toFixed(n)It converts a number into a string by retaining n decimal places.<br \/>\nExample:<br \/>\nvar number = 2.2689;<br \/>\nvar Num = number.toFixed(2);<br \/>\nNum=2.27<\/p>\n<table style=\"table-layout: fixed; width: auto; font-size: 100%;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<th valign=\"top\">Methods<\/th>\n<th valign=\"top\"><strong>Description<\/strong><\/th>\n<\/tr>\n<tr>\n<td valign=\"top\">big( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;big&gt;&lt;\/big&gt; to the string displays bigger text.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">bold( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;bold&gt;&lt;\/bold&gt; to the string displays bold text.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">fixed( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;tt&gt;&lt;\/tt&gt; to the string makes the text appears in typewriter style.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">ilatics( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;i&gt;&lt;\/i&gt; to the string displays text in italics.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">small( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;small&gt;&lt;\/small&gt; to the string displays smaller text.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">sub( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;sub&gt;&lt;\/sub&gt; to the string and displays the text as subscript text.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">sup( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;sup&gt;&lt;\/sup&gt; to the string and displays the text as superscript text.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">strike( )<\/td>\n<td valign=\"top\">Add the HTML tags &lt;strike&gt;&lt;\/strike&gt; to the string and displays the text as strike-through text.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">fontcolor(color)<\/td>\n<td valign=\"top\">Add the HTML tags &lt;fontcolor(color)&gt;&lt;\/font&gt; to the string and displays the text in predefined color.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">fontcolor(size)<\/td>\n<td valign=\"top\">Add the HTML tags &lt;fontcolor(size)&gt;&lt;\/font&gt; to the string and displays the text in predefined size.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">toLowerCase( )<\/td>\n<td valign=\"top\">Convert string to all lowercase<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">toUpperCase<\/td>\n<td valign=\"top\">Convert string to all\u00a0uppercase<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">substring(A, B)<\/td>\n<td valign=\"top\">Extracts the substring starting from position A to position B.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"264\">chartAt(index)<\/td>\n<td valign=\"top\">Returns the character located at position index within the string (index with 0)<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">concat(text)<\/td>\n<td valign=\"top\">Add text to the end of the string<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">toString( )<\/td>\n<td valign=\"top\">Returns the greatest integer less than or equal to x<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">fromCharCode()<\/td>\n<td valign=\"top\">Convert a Unicode number into a character<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The syntax of using the String method is\u00a0 <strong>String.Method<\/strong>, where string can be of any acceptable variable name in JavaScript.<\/p>\n<p>For example,\u00a0 You can define the following string:<\/p>\n<p>var StudentName=&#8221;John Peterson&#8221;<\/p>\n<p>and manipulate the string with various methods below:<\/p>\n<ul>\n<li>StudentName.toLowerCase( )\u00a0\u00a0 converts the string to\u00a0 john peterson<\/li>\n<li>StudentName.toUpperrCase( )\u00a0 converts the string to\u00a0 JOHN PETERSON<\/li>\n<li>StudentName.bold( )\u00a0 displays the string as\u00a0\u00a0<strong>John Peterson<\/strong><\/li>\n<li>StudentName.italics( )\u00a0 displays the string as\u00a0\u00a0<em>John Peterson<\/em><\/li>\n<li>StudentName.sub( )\u00a0 displays the string as\u00a0\u00a0<sub>John Peterson<\/sub><\/li>\n<li>StudentName.sup( )\u00a0 displays the string as\u00a0\u00a0<sup>John Peterson<\/sup><\/li>\n<li>StudentName.strike( )\u00a0 displays the string as\u00a0\u00a0<span style=\"text-decoration: line-through;\">John Peterson<\/span><\/li>\n<li>StudentName.substring( 1,6)\u00a0 display part of the string as\u00a0 ohn p<\/li>\n<\/ul>\n<p>The following example demonstrates the usage of String methods .<\/p>\n<h4><strong>Example 11.1<\/strong><\/h4>\n<pre style=\"font-size: 100%;\">&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=windows-1252\"&gt;\r\n&lt;title&gt;String Methods&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\nmyString=\"JavaScript Tutorial\";\r\n\r\nIString=myString.italics();\r\nSString=myString.small();\r\nBString=myString.bold();\r\nBigString=myString.big();\r\nFixString=myString.fixed();\r\nsubString=myString.sub();\r\nsupString=myString.sup();\r\nstrikeString=myString.strike();\r\ncolorString=myString.fontcolor(\"red\");\r\nStringSize=myString.fontsize(\"+3\");\r\nLCstring=myString.toLowerCase();\r\nUCstring=myString.toUpperCase();\r\nPString=myString.substring(1,5);\r\nCharString=myString.charAt(8);\r\nconcatString=myString.concat(\" for you\")\r\nSameString=myString.toString();\r\ndocument.write(\"&lt;br&gt;Text in Italics : \"+IString);\r\ndocument.write(\"&lt;br&gt;Smaller Text : \"+SString);\r\ndocument.write(\"&lt;br&gt;Text in Bold : \"+BString);\r\ndocument.write(\"&lt;br&gt;Text with bigger characters : \"+BigString);\r\ndocument.write(\"&lt;br&gt;Text with typewriter style : \"+FixString);\r\ndocument.write(\"&lt;br&gt;Subscript text: \"+subString);\r\ndocument.write(\"&lt;br&gt;Superscript text: \"+supString);\r\ndocument.write(\"&lt;br&gt;Strike-thru text: \"+strikeString);\r\ndocument.write(\"&lt;br&gt;Red color text: \"+colorString);\r\ndocument.write(\"&lt;br&gt;Fontsize +3: \"+StringSize);\r\ndocument.write(\"&lt;br&gt;Convert to lowercase: \"+LCstring);\r\ndocument.write(\"&lt;br&gt;Convert to Uppercase: \"+UCstring);\r\ndocument.write(\"&lt;br&gt;Extracts SubString: \"+PString);\r\ndocument.write(\"&lt;br&gt;Character at 9th position is : \"+CharString);\r\ndocument.write(\"&lt;br&gt;Concatenates String: \"+concatString);\r\ndocument.write(\"&lt;br&gt;Same String: \"+SameString);\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;&lt;\/html&gt;\r\n\r\n<\/pre>\n<p><strong>Click<a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example11.1.htm\"> Example 11.1<\/a> to see the output<\/strong><\/p>\n<h4><strong>Example 11.2: The fromCharCode() Method<\/strong><\/h4>\n<p>This example demonstrates the usage of the fromCharCode() Method. This method converts a Unicode into a corresponding character.<\/p>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body onload=\"myFunction()\"&gt;\r\n\r\n&lt;h3&gt;The character of unicode &lt;b id=\"chr\"&gt;&lt;\/b&gt; is &lt;b id=\"chr1\"&gt;&lt;\/b&gt;&lt;\/h3&gt;\r\n\r\n&lt;script&gt;\r\nfunction myFunction() {\r\nvar ucode = prompt(\"Please enter a Unicode and click OK to view the corresponding character\");\r\nvar myChr = String.fromCharCode(ucode);\r\ndocument.getElementById(\"chr1\").innerHTML = myChr;\r\ndocument.getElementById(\"chr\").innerHTML = ucode;\r\n}\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><input type=\"button\" value=\"View Output\" \/><\/p>\n<h3>The character of unicode <b id=\"chr1\"><\/b> is <b id=\"chr\"><\/b><\/h3>\n<p><script>\nfunction myFunction() {\n    var ucode = prompt(\"Please enter a Unicode and click OK to view the corresponding character\");\n    var myChr = String.fromCharCode(ucode);\n\tdocument.getElementById(\"chr\").innerHTML = myChr;\n\tdocument.getElementById(\"chr1\").innerHTML = ucode;\n}\n<\/script><\/p>\n<h4><strong>Example 11.2: The charCodeAt(0) Method<\/strong><\/h4>\n<p>This method returns the Unicode of the first character in a string. The index 0 indicates the first position of the string. This example returns the Unicode for the character input by the user. As the user only input one character, the index 0 indicates the character being entered.<\/p>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body onload=\"myFunction()\"&gt;\r\n\r\n&lt;h3&gt;The unicode of the character &lt;b id=\"chr\"&gt;&lt;\/b&gt; is &lt;b id=\"chr1\"&gt;&lt;\/b&gt;&lt;\/h3&gt;\r\n\r\n&lt;script&gt;\r\nfunction myFunction2() {\r\n var mychr = prompt(\"Please enter a character and click OK to view the corresponding unicode\");\r\n document.getElementById(\"chr\").innerHTML = mychr;\r\n document.getElementById(\"chr1\").innerHTML = mychr.charCodeAt(0);\r\n}\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><input type=\"button\" value=\"View output\" \/><\/p>\n<h3>The Unicode of the character <b id=\"chr2\"><\/b> is <b id=\"chr3\"><\/b><\/h3>\n<p><script>\nfunction myFunction2() {\n    var mychr = prompt(\"Please enter a character and click OK to view the corresponding unicode\");\n    document.getElementById(\"chr2\").innerHTML = mychr;\n\tdocument.getElementById(\"chr3\").innerHTML = mychr.charCodeAt(0);\n}\n<\/script><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 align=\"center\"><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-10-objects-part-2-math-object\/\">&lt;Lesson 10&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-12-objects-part-4-document-object\/\">&lt;Lesson 12&gt;<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Lesson 10&gt; [Contents] &lt;Lesson 12&gt; The String object in JavaScript is for manipulating texts and characters.\u00a0 It can be used to process names, addresses, phone numbers, ID, company names, serial numbers and so on. As a matter of facts,\u00a0 JavaScript statements comprise strings and characters. A string is made up of letters, numbers and alphanumeric &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-11-objects-part-3-string-object\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 11: The String Object&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-344","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 11: The String Object - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"description\" content=\"This JavaScript Lesson illustrates how to manipulate string object\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 11: The String Object - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"This JavaScript Lesson illustrates how to manipulate string object\" \/>\n<meta property=\"og:url\" content=\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-18T05:43:55+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-11-objects-part-3-string-object\/\",\"url\":\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html\",\"name\":\"JavaScript Lesson 11: The String Object - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2011-12-20T10:06:35+00:00\",\"dateModified\":\"2017-11-18T05:43:55+00:00\",\"description\":\"This JavaScript Lesson illustrates how to manipulate string object\",\"breadcrumb\":{\"@id\":\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 11: The String Object\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 11: The String Object - Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"This JavaScript Lesson illustrates how to manipulate string object","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 11: The String Object - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"This JavaScript Lesson illustrates how to manipulate string object","og_url":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-18T05:43:55+00:00","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-11-objects-part-3-string-object\/","url":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html","name":"JavaScript Lesson 11: The String Object - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2011-12-20T10:06:35+00:00","dateModified":"2017-11-18T05:43:55+00:00","description":"This JavaScript Lesson illustrates how to manipulate string object","breadcrumb":{"@id":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/javascript-tutor.net\/jstutor\/jstutor_lesson11.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 11: The String Object"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":97,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/344\/revisions"}],"predecessor-version":[{"id":2490,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/344\/revisions\/2490"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}