脚本宝典收集整理的这篇文章主要介绍了js实例String(字符串)对象,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
返回字符串的长度
源代码
<!DOCTYPE htML> <html> <body> <script> VAR txt = "Hello World!"; document.wrITe(txt.length); </script> </body> </html>
运行结果
为字符串添加样式
源代码
<!DOCTYPE html> <html> <body> <script> var txt = "Hello World!"; document.write("<p>Big: " + txt.big() + "</p>"); document.write("<p>Small: " + txt.small() + "</p>"); document.write("<p>Bold: " + txt.bold() + "</p>"); document.write("<p>Italic: " + txt.italics() + "</p>"); document.write("<p>Fixed: " + txt.fixed() + "</p>"); document.write("<p>Strike: " + txt.strike() + "</p>"); document.write("<p>Fontcolor: " + txt.fontcolor("green") + "</p>"); document.write("<p>FontSize: " + txt.fontsize(6) + "</p>"); document.write("<p>Subscript: " + txt.sub() + "</p>"); document.write("<p>Superscript: " + txt.sup() + "</p>"); document.write("<p>Link: " + txt.link("https://www.w3cschool.cc") + "</p>"); document.write("<p>Blink: " + txt.blink() + " (does not work in IE, Chrome, or Safari)</p>"); </script> </body> </html>
运行结果
返回字符串中指定文本首次出现的位置 - indexOf()方法
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to locate where in the string a specifed value occurs.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str="Hello world, welcome to the universe."; var n=str.indexOf("welcome"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
运行结果
查找字符串中特定的字符,若找到,则返回该字符 - match() 方法
源代码
<!DOCTYPE html> <html> <body> <script> var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); document.write(str.match("worlld") + "<br>"); document.write(str.match("world!")); </script> </body> </html>
运行结果
替换字符串中的字符 - replace()
源代码
<!DOCTYPE html> <html> <body> <p>Click the button to replace "Microsoft" with "W3CSchool" in the paragraph below:</p> <p id="demo">Visit Microsoft!</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str=document.getElementById("demo").innerHTML; var n=str.replace("Microsoft","W3CSchool"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
运行结果
返回字符串的长度
源代码
<!DOCTYPE html> <html> <body> <script> var txt = "Hello World!"; document.write(txt.length); </script> </body> </html>
运行结果
为字符串添加样式
源代码
<!DOCTYPE html> <html> <body> <script> var txt = "Hello World!"; document.write("<p>Big: " + txt.big() + "</p>"); document.write("<p>Small: " + txt.small() + "</p>"); document.write("<p>Bold: " + txt.bold() + "</p>"); document.write("<p>Italic: " + txt.italics() + "</p>"); document.write("<p>Fixed: " + txt.fixed() + "</p>"); document.write("<p>Strike: " + txt.strike() + "</p>"); document.write("<p>Fontcolor: " + txt.fontcolor("green") + "</p>"); document.write("<p>Fontsize: " + txt.fontsize(6) + "</p>"); document.write("<p>Subscript: " + txt.sub() + "</p>"); document.write("<p>Superscript: " + txt.sup() + "</p>"); document.write("<p>Link: " + txt.link("https://www.w3cschool.cc") + "</p>"); document.write("<p>Blink: " + txt.blink() + " (does not work in IE, Chrome, or Safari)</p>"); </script> </body> </html>
运行结果
返回字符串中指定文本首次出现的位置 - indexOf()方法
源代码
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to locate where in the string a specifed value occurs.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str="Hello world, welcome to the universe."; var n=str.indexOf("welcome"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
运行结果
查找字符串中特定的字符,若找到,则返回该字符 - match() 方法
源代码
<!DOCTYPE html> <html> <body> <script> var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); document.write(str.match("worlld") + "<br>"); document.write(str.match("world!")); </script> </body> </html>
运行结果
替换字符串中的字符 - replace()
源代码
<!DOCTYPE html> <html> <body> <p>Click the button to replace "Microsoft" with "W3CSchool" in the paragraph below:</p> <p id="demo">Visit Microsoft!</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str=document.getElementById("demo").innerHTML; var n=str.replace("Microsoft","W3CSchool"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
运行结果
觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的js实例String(字符串)对象全部内容,希望文章能够帮你解决js实例String(字符串)对象所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。