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 对象属性
String 对象方法
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
- javaScript核心对象--------------string
- JavaScript的String对象
- javascript 常用对象--- String
- JavaScript String 对象参考
- Javascript中的String对象
- JavaScript String 对象参考手册
- JavaScript String 对象参考手册
- Javascript String对象
- javascript:String 对象
- JavaScript String 对象 toUpperCase()
- JavaScript String 对象
- javascript string对象
- javascript String 对象
- [JavaScript]String 对象
- javascript String 对象介绍
- Javascript之String对象
- JavaScript String(字符串)对象
- JavaScript String 对象参考手册
- UITableView: textLabel,detailTextLabel
- SQL Server bcp 及 NullIf 的使用
- 模板类声明和定义为何要写一起
- 内存分析工具 MAT 的使用
- 删除老的 provisioning profile 如何删除xcode中的多余证书
- JavaScript String 对象
- android大扫盲之SurfaceView,SurfaceHolder,SurfaceHolder.CallBack.
- 【bdump】bdump目录下产生大量cdmp_2014xxx和死锁信息
- android release export has encountered a problem
- error LNK2001: unresolved external symbol "__declspec(dllimport) const CNoteMgr::`vftable'"
- 年夜揭秘上网站优化的细则
- codeforces 2-A Winner
- 原谅我,无法记住你
- linux下的 jmap命令