JavaScript String 对象

来源:互联网 发布:防伪底纹设计软件 编辑:程序博客网 时间:2024/05/18 21:39

JavaScript String 对象

String 对象

String 对象用于处理文本(字符串)。

创建 String 对象的语法:

new String(s);String(s);

参数

参数 s 是要存储在 String 对象中或转换成原始字符串的值。

返回值

当 String() 和运算符 new 一起作为构造函数使用时,它返回一个新创建的 String 对象,存放的是字符串 s 或 s 的字符串表示。

当不用 new 运算符调用 String() 时,它只把 s 转换成原始的字符串,并返回转换后的值。

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()返回某个字符串对象的原始值。

String 对象描述

字符串是 JavaScript 的一种基本的数据类型。

String 对象的 length 属性声明了该字符串中的字符数。

String 类定义了大量操作字符串的方法,例如从字符串中提取字符或子串,或者检索字符或子串。

需要注意的是,JavaScript 的字符串是不可变的(immutable),String 类定义的方法都不能改变字符串的内容。像 String.toUpperCase() 这样的方法,返回的是全新的字符串,而不是修改原始字符串。

在较早的 Netscape 代码基的 JavaScript 实现中(例如 Firefox 实现中),字符串的行为就像只读的字符数组。例如,从字符串 s 中提取第三个字符,可以用 s[2] 代替更加标准的 s.charAt(2)。此外,对字符串应用 for/in 循环时,它将枚举字符串中每个字符的数组下标(但要注意,ECMAScript 标准规定,不能枚举 length 属性)。因为字符串的数组行为不标准,所以应该避免使用它。

JavaScript String(字符串)对象 实例


计算字符串的长度
<html><body><script type="text/javascript">var txt="Hello World!"document.write(txt.length)</script></body></html>

为字符串添加样式
<html><body><script type="text/javascript">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>Blink: " + txt.blink() + " (does not work in IE)</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("http://www.w3school.com.cn") + "</p>")</script></body></html>

效果:


indexOf() 方法
<html><body><script type="text/javascript">var str="Hello world!"document.write(str.indexOf("Hello") + "<br />")document.write(str.indexOf("World") + "<br />")document.write(str.indexOf("world"))</script></body></html>

match() 方法
<html><body><script type="text/javascript">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()
<html><body><script type="text/javascript">var str="Visit Microsoft!"document.write(str.replace(/Microsoft/,"W3School"))</script></body></html>


0 0
原创粉丝点击