核心提示:!DOCTYPE htmlhtmlheadmeta charset=UTF-8titleString对象方法/title/headbody style=height: 2000px;/bodyscri...
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>String对象方法</title> </head> <body style="height: 2000px;"> </body> <script type="text/javascript"> //创建 HTML 锚。 var str="Hello world!"; document.write("anchor(): "+str.anchor("myanchor")+'<br>'); //用大号字体显示字符串。 document.write("big(): "+str.big()+'<br>'); //用于把字符串显示为粗体。 document.write("bold(): "+str.bold()+'<br>'); //返回指定位置的字符。 document.write("charAt(): "+str.charAt(1)+'<br>'); //返回在指定的位置的字符的 Unicode 编码。 document.write("charCodeAt(): "+str.charCodeAt(1)+'<br>'); //用于连接两个或多个字符串。 var str1="Hello " var str2="world!" document.write("concat(): "+str1.concat(str2)+'<br>'); //以打字机文本显示字符串。 document.write("fixed(): "+str.fixed()+'<br>'); //使用指定的颜色来显示字符串。 document.write("fontcolor(): "+str.fontcolor("Red")+'<br>'); //使用指定的尺寸来显示字符串。 document.write("fontsize(): "+str.fontsize(7)+'<br>'); //从字符编码创建一个字符串。 document.write("fromCharCode(): "+String.fromCharCode(72,69,76,76,79)+'<br>'); //检索字符串。 document.write("indexOf(): "+str.indexOf("Hello") + "<br />"); document.write("indexOf(): "+str.indexOf("World") + "<br />"); document.write("indexOf(): "+str.indexOf("world")+ "<br />"); //使用斜体显示字符串。 document.write("italics(): "+str.italics()+'<br>'); //从后向前搜索字符串。 document.write("lastIndexOf(): "+str.lastIndexOf("Hello") + "<br />"); document.write("lastIndexOf(): "+str.lastIndexOf("World") + "<br />"); document.write("lastIndexOf(): "+str.lastIndexOf("world") + "<br />"); //将字符串显示为链接。 document.write("link(): "+str.link("https://www.w3school.com.cn")+ "<br />"); //用本地特定的顺序来比较两个字符串。 var str2="Hello world!!!!!!"; var str3="Hello world!"; document.write("localeCompare(): "+str2.localeCompare(str)+ "<br />"); document.write("localeCompare(): "+str.localeCompare(str3)+ "<br />"); //找到一个或多个正则表达式的匹配。 document.write("match(): "+str.match("world") + "<br />"); document.write("match(): "+str.match("World") + "<br />"); document.write("match(): "+str.match("worlld") + "<br />"); document.write("match(): "+str.match("world!") + "<br />"); //替换与正则表达式匹配的子串。 var str4="Visit Microsoft!" document.write("replace(): "+str4.replace(/Microsoft/, "W3School")+ "<br />"); //检索与正则表达式相匹配的值。 var str5="Visit W3School!" document.write("search(): "+str5.search(/W3School/)+ "<br />"); //提取字符串的片断,并在新的字符串中返回被提取的部分。 var str="Hello happy world!" document.write("slice(): "+str.slice(6)+ "<br />"); //使用小字号来显示字符串。 document.write("small(): "+str.small()+ "<br />"); //把字符串分割为字符串数组。 var str="How are you doing today?" document.write("split(): "+str.split(" ") + "<br />"); document.write("split(): "+str.split("") + "<br />"); document.write("split(): "+str.split(" ",3)+ "<br />"); //使用删除线来显示字符串。 var str="Hello world!" document.write("strike(): "+str.strike()+ "<br />"); //把字符串显示为下标。 document.write("sub(): "+str.sub()+ "<br />"); //从起始索引号提取字符串中指定数目的字符 document.write("substr(): "+str.substr(3)+ "<br />"); var str="Hello world!" document.write("substr(): "+str.substr(3,7)+ "<br />"); //提取字符串中两个指定的索引号之间的字符。 document.write("substring(): "+str.substring(3)+ "<br />"); var str="Hello world!" document.write("substring(): "+str.substring(3,7)+ "<br />"); //把字符串显示为上标。 document.write("sup(): "+str.sup()+ "<br />") //把字符串转换为小写。 document.write("toLocaleLowerCase(): "+str.toLocaleLowerCase()+ "<br />") //把字符串转换为大写。 document.write("toLocaleUpperCase(): "+str.toLocaleUpperCase()+ "<br />") //把字符串转换为小写。 document.write("toLowerCase(): "+str.toLowerCase()+ "<br />") //把字符串转换为大写。 document.write("toUpperCase(): "+str.toUpperCase()+ "<br />") </script> </html>