这篇文章主要为大家展示了“JavaScript如何处理已有的字符块”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript如何处理已有的字符块”这篇文章吧。

JavaScript 字符串对象
JavaScript String(字符串)对象用于处理已有的字符块。

String 对象 属性及描述
constructor:对创建该对象的函数的引用
length:字符串的长度
prototype:允许您向对象添加属性和方法

String 对象 方法及描述

anchor()//创建HTML锚。big()//用大号字体显示字符串。blink()//显示闪动字符串。bold()//使用粗体显示字符串。charAt()//返回在指定位置的字符。charCodeAt()//返回在指定的位置的字符的Unicode编码。concat()//连接字符串。fixed()//以打字机文本显示字符串。fontcolor()//使用指定的颜色来显示字符串。fontsize()//使用指定的尺寸来显示字符串。fromCharCode()//从字符编码创建一个字符串。indexOf()//检索字符串。italics()//使用斜体显示字符串。lastIndexOf()//从后向前搜索字符串。link()//将字符串显示为链接。localeCompare()//用本地特定的顺序来比较两个字符串。match()//找到一个或多个正则表达式的匹配。replace()//替换与正则表达式匹配的子串。search()//检索与正则表达式相匹配的值。slice()//提取字符串的片断,并在新的字符串中返回被提取的部分。small()//使用小字号来显示字符串。split()//把字符串分割为字符串数组。strike()//使用删除线来显示字符串。sub()//把字符串显示为下标。substr()//从起始索引号提取字符串中指定数目的字符。substring()//提取字符串中两个指定的索引号之间的字符。sup()//把字符串显示为上标。toLocaleLowerCase()//把字符串转换为小写。toLocaleUpperCase()//把字符串转换为大写。toLowerCase()//把字符串转换为小写。toUpperCase()//把字符串转换为大写。toSource()//代表对象的源代码。toString()//返回字符串。valueOf()//返回某个字符串对象的原始值。


使用长度属性计算字符串的长度:

<html><body><scripttype="text/javascript">vartxt="HelloWorld!"document.write(txt.length)</script></body></html>


为字符串添加样式:

<html><body><scripttype="text/javascript">vartxt="HelloWorld!"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>Blink:"+txt.blink()+"(doesnotworkinIE)</p>")document.write("<p>Fixed:"+txt.fixed()+"</p>")document.write("<p>Strike:"+txt.strike()+"</p>")document.write("<p>Fontcolor:"+txt.fontcolor("Red")+"</p>")document.write("<p>Fontsize:"+txt.fontsize(16)+"</p>")document.write("<p>Lowercase:"+txt.toLowerCase()+"</p>")document.write("<p>Uppercase:"+txt.toUpperCase()+"</p>")document.write("<p>Subscript:"+txt.sub()+"</p>")document.write("<p>Superscript:"+txt.sup()+"</p>")document.write("<p>Link:"+txt.link("..")+"</p>")</script></body></html>


indexOf() 方法:

<html><body><scripttype="text/javascript">varstr="Helloworld!"document.write(str.indexOf("Hello")+"<br/>")document.write(str.indexOf("World")+"<br/>")document.write(str.indexOf("world"))</script></body></html>

注释:使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置。

match() 方法:

<html><body><scripttype="text/javascript">varstr="Helloworld!"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>

注释:使用 match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

替换字符串中的字符 - replace():

<html><body><scripttype="text/javascript">varstr="VisitMicrosoft!"document.write(str.replace(/Microsoft/,"HuluMiao"))</script></body></html>

注释:使用 replace() 方法在字符串中用某些字符替换另一些字符。

以上是“JavaScript如何处理已有的字符块”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!