{"id":26,"date":"2007-05-21T00:22:21","date_gmt":"2007-05-21T04:22:21","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=26"},"modified":"2007-08-25T14:07:39","modified_gmt":"2007-08-25T18:07:39","slug":"js-string","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-string","title":{"rendered":"JavaScript\u7684\u5b57\u7b26\u4e32String"},"content":{"rendered":"<p>\u5b57\u7b26\u4e32\u5bf9\u8c61(String)\u88ab\u7528\u6765\u5904\u7406\u4e00\u6bb5\u50a8\u5b58\u7684\u6587\u672c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u8303\u4f8b<\/strong><br \/>\n\u4e0b\u9762\u5217\u51fa\u5e38\u7528\u7684String\u5c5e\u6027\u548c\u51fd\u6570\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u4f7f\u7528\u8303\u4f8b<\/p>\n<ul>\n<li>\u4f7f\u7528length\u5c5e\u6027\u83b7\u5f97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<br \/>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">var txt=&#8221;Hello world!&#8221;<br \/>\ndocument.write(txt.length)<\/td>\n<\/tr>\n<\/table>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u4e3a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">12<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>\u4f7f\u7528toUpperCase()\u51fd\u6570\u4f7f\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u5168\u90e8\u53d8\u4e3a\u5927\u5199<br \/>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">var txt=&#8221;Hello world!&#8221;<br \/>\ndocument.write(txt.toUpperCase())<\/td>\n<\/tr>\n<\/table>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u4e3a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">HELLO WORLD!<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>\u5b57\u7b26\u4e32\u663e\u793a\u63a7\u5236<br \/>\n\u5c06\u4e0b\u9762\u4ee3\u7801\u62f7\u8d1d\u5b58\u4e3ahtml\u6587\u4ef6\uff0c\u7528\u6d4f\u89c8\u5668\u6253\u5f00\u5bdf\u770b\u6548\u679c\u3002<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;var txt=&#8221;Hello World!&#8221;<\/p>\n<p>document.write(&#8220;&lt;p&gt;Big: &#8221; + txt.big() + &#8220;&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Small: &#8221; + txt.small() + &#8220;&lt;\/p&gt;&#8221;)<\/p>\n<p>document.write(&#8220;&lt;p&gt;Bold: &#8221; + txt.bold() + &#8220;&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Italic: &#8221; + txt.italics() + &#8220;&lt;\/p&gt;&#8221;)<\/p>\n<p>document.write(&#8220;&lt;p&gt;Blink: &#8221; + txt.blink() + &#8221; (does not work in IE)&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Fixed: &#8221; + txt.fixed() + &#8220;&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Strike: &#8221; + txt.strike() + &#8220;&lt;\/p&gt;&#8221;)<\/p>\n<p>document.write(&#8220;&lt;p&gt;Fontcolor: &#8221; + txt.fontcolor(&#8220;Red&#8221;) + &#8220;&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Fontsize: &#8221; + txt.fontsize(16) + &#8220;&lt;\/p&gt;&#8221;)<\/p>\n<p>document.write(&#8220;&lt;p&gt;Lowercase: &#8221; + txt.toLowerCase() + &#8220;&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Uppercase: &#8221; + txt.toUpperCase() + &#8220;&lt;\/p&gt;&#8221;)<\/p>\n<p>document.write(&#8220;&lt;p&gt;Subscript: &#8221; + txt.sub() + &#8220;&lt;\/p&gt;&#8221;)<br \/>\ndocument.write(&#8220;&lt;p&gt;Superscript: &#8221; + txt.sup() + &#8220;&lt;\/p&gt;&#8221;)<\/p>\n<p>document.write(&#8220;&lt;p&gt;Link: &#8221; + txt.link(&#8220;http:\/\/www.prglab.com&#8221;) + &#8220;&lt;\/p&gt;&#8221;)<br \/>\n&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>indexOf()\u51fd\u6570<br \/>\n\u4e0b\u9762\u7684\u4f8b\u5b50\u4f7f\u7528indexOf()\u51fd\u6570\u8fd4\u56de\u5728\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u6307\u5b9a\u5b50\u4e32\u7684\u4f4d\u7f6e\u3002\u6ce8\u610f\u4f4d\u7f6e\u7d22\u5f15\u53f7\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u5982\u679c\u6307\u5b9a\u5b50\u4e32\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de-1<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;var str=&#8221;Hello world!&#8221;<br \/>\ndocument.write(str.indexOf(&#8220;Hello&#8221;) + &#8220;&lt;br \/&gt;&#8221;)<br \/>\ndocument.write(str.indexOf(&#8220;World&#8221;) + &#8220;&lt;br \/&gt;&#8221;)<br \/>\ndocument.write(str.indexOf(&#8220;world&#8221;))<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p>\u8f93\u51fa\u4e3a\uff1a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">0<br \/>\n-1<br \/>\n6<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>match()\u51fd\u6570<br \/>\nmatch\u51fd\u6570\u4e0e\u4e0a\u9762\u7684indexOf()\u51fd\u6570\u7c7b\u4f3c\uff0c\u4e5f\u662f\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6307\u5b9a\u7684\u5b50\u4e32\uff0c\u4f46\u4e0d\u540c\u7684\u662f\u5982\u679c\u627e\u5230\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u8be5\u5b50\u4e32\u7684\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u503c(null)<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;var str=&#8221;Hello world!&#8221;<br \/>\ndocument.write(str.match(&#8220;world&#8221;) + &#8220;&lt;br \/&gt;&#8221;)<br \/>\ndocument.write(str.match(&#8220;World&#8221;) + &#8220;&lt;br \/&gt;&#8221;)<br \/>\ndocument.write(str.match(&#8220;worlld&#8221;) + &#8220;&lt;br \/&gt;&#8221;)<br \/>\ndocument.write(str.match(&#8220;world!&#8221;))<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p>\u8f93\u51fa\u4e3a\uff1a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">world<br \/>\nnull<br \/>\nnull<br \/>\nworld!<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>\u66ff\u6362\u51fd\u6570replace()<br \/>\n\u5c06\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u7684\u5b50\u4e32\u7531\u5176\u4ed6\u5b57\u7b26\u4e32\u4ee3\u66ff<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;var str=&#8221;Hello John!&#8221;<br \/>\ndocument.write(str.replace(\/John\/,&#8221;Jane&#8221;))<\/p>\n<p>&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p>\u8f93\u51fa\u4e3a\uff1a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">Hello Jane!<\/td>\n<\/tr>\n<\/table>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u7b26\u4e32\u5bf9\u8c61(String)\u88ab\u7528\u6765\u5904\u7406\u4e00\u6bb5\u50a8\u5b58\u7684\u6587\u672c\u3002 \u4f7f\u7528\u8303\u4f8b \u4e0b\u9762\u5217\u51fa\u5e38\u7528\u7684String\u5c5e\u6027\u548c\u51fd\u6570\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u4f7f\u7528\u8303\u4f8b \u4f7f\u7528length\u5c5e\u6027\u83b7\u5f97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6 var txt=&#8221;Hello world!&#8221; document.write(txt.length) \u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u4e3a 12 \u4f7f\u7528toUpperCase()\u51fd\u6570\u4f7f\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u5168\u90e8\u53d8\u4e3a\u5927\u5199 var txt=&#8221;Hello world!&#8221; document.write(txt.toUpperCase()) \u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u4e3a HELLO WORLD! \u5b57\u7b26\u4e32\u663e\u793a\u63a7\u5236 \u5c06\u4e0b\u9762\u4ee3\u7801\u62f7\u8d1d\u5b58\u4e3ahtml\u6587\u4ef6\uff0c\u7528\u6d4f\u89c8\u5668\u6253\u5f00\u5bdf\u770b\u6548\u679c\u3002 &lt;html&gt; &lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;var txt=&#8221;Hello World!&#8221; document.write(&#8220;&lt;p&gt;Big: &#8221; + txt.big() + &#8220;&lt;\/p&gt;&#8221;) document.write(&#8220;&lt;p&gt;Small: &#8221; + txt.small() + &#8220;&lt;\/p&gt;&#8221;) document.write(&#8220;&lt;p&gt;Bold: &#8221; + txt.bold() + &#8220;&lt;\/p&gt;&#8221;) document.write(&#8220;&lt;p&gt;Italic: &#8221; + txt.italics() + &#8220;&lt;\/p&gt;&#8221;) document.write(&#8220;&lt;p&gt;Blink: &#8221; + txt.blink() + &#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":19,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/26"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/26\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}