JavaScript 字符串(String)对象
String 对象用于处理已有的字符块。
一个字符串可以使用单引号或双引号。
使用位置(索引)可以访问字符串中任何的字符。字符串的索引从零开始, 所以字符串第一字符为 [0],第二个字符为 [1], 等等。
可以在字符串中使用引号。字符串由单引号包括时,字符串可以直接使用双引号;字符串由双引号包括时,字符串可以直接使用单引号。
或者在字符串中使用转义字符。
转义字符
代码 | 输出 |
\' | 单引号 |
\" | 双引号 |
\\ | 反斜杠 |
\n | 换行 |
\r | 回车 |
\t | tab(制表符) |
\b | 退格符 |
\f | 换页符 |
String对象属性
属性 | 描述 |
constructor | 对创建该对象的函数的引用 |
length | 字符串的长度 |
prototype | 允许您向对象添加属性和方法 |
String对象方法
方法 | 描述 |
charAt() | 返回在指定位置的字符。 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 |
concat() | 连接两个或更多字符串,并返回新的字符串。 |
fromCharCode() | 将 Unicode 编码转为字符。 |
indexOf() | 返回某个指定的字符串值在字符串中首次出现的位置。 |
lastIndexOf() | 从后向前搜索字符串。 |
match() | 查找找到一个或多个正则表达式的匹配。 |
replace() | 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串。 |
search() | 查找与正则表达式相匹配的值。 |
slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分。 |
split() | 把字符串分割为字符串数组。 |
substr() | 从起始索引号提取字符串中指定数目的字符。 |
substring() | 提取字符串中两个指定的索引号之间的字符。 |
toLowerCase() | 把字符串转换为小写。 |
toUpperCase() | 把字符串转换为大写。 |
trim() | 去除字符串两边的空白。 |
valueOf() | 返回某个字符串对象的原始值。 |
测试代码
var txt = 'Hello World!'; document.writeln('<h3>测试字符中为: "' + txt + '", 变量名为txt。</h3>'); //字符串属性length的使用 document.writeln('<p>' + 'length属性的值是:' + txt.length + '</p>'); //函数charAt(n),返回字符中指定位置的字符 document.writeln('<p>' + 'txt.charAt(4) = ' + txt.charAt(6) + '</p>'); document.writeln('<p>' + 'txt.charAt(20) = ' + txt.charAt(20) + '</p>'); //函数replace(searchvalue,newvalue), 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。 document.writeln('<p>' + 'txt.replace("World", "Test") = ' + txt.replace('World', 'Test') + '</p>'); document.writeln('<p>' + 'txt.replace(/o/g, "a") = ' + txt.replace(/o/g, 'a') + '</p>'); //函数split(separater [,limit]), 用于把一个字符串分割成字符串数组。 document.writeln('<p>' + 'txt.split(" ") = ' + txt.split(' ') + '</p>'); document.writeln('<p>' + 'txt.split(" ", 1) = ' + txt.split(' ', 1) + '</p>'); / //字符串检索 / //函数indexOf(str),返回某个指定的字符串在字符串中首次出现的位置 document.writeln('<p>' + 'txt.indexOf("World") = ' + txt.indexOf('World') + '</p>'); document.writeln('<p>' + 'txt.indexOf("world") = ' + txt.indexOf('world') + ' , 查找区分大小的,没有找到返回-1。</p>'); document.writeln('<p>' + 'txt.indexOf("o") = ' + txt.indexOf('o') + '</p>'); document.writeln('<p>' + 'txt.indexOf("o", 6) = ' + txt.indexOf('o', 1) + '</p>'); //函数lastIndexOf(str),从后向前搜索字符串 document.writeln('<p>' + 'txt.lastIndexOf("o") = ' + txt.lastIndexOf('o') + '</p>'); //函数search(searchvalue), 用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。 document.writeln('<p>' + 'txt.search("or") = ' + txt.search('or') + '</p>'); document.writeln('<p>' + 'txt.search(/o/g) = ' + txt.search(/o/g) + '</p>'); document.writeln('<p>' + 'txt.search(/z/g) = ' + txt.search(/z/g) + '</p>'); //函数match(regexp),在字符串内检索指定的值,或找到一个或多个正则表达式的匹配 document.writeln('<p>' + 'txt.match("or") = ' + txt.match('or') + '</p>'); document.writeln('<p>' + 'txt.match(/o/g) = ' + txt.match(/o/g) + '</p>'); document.writeln('<p>' + 'txt.match(/z/g) = ' + txt.match(/z/g) + ' , 没有找到返回null</p>'); / //字符串截位 / //函数substr(start [,length]), 可在字符串中抽取从start下标开始的指定数目的字符。 //函数slice(start [,end]),方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。 //函数substring(start [,end]),方法返回的子串包括start处的字符,但不包括end处的字符。 document.writeln('<p>' + 'txt.substr(4) = ' + txt.substr(4) + '</p>'); document.writeln('<p>' + 'txt.slice(4) = ' + txt.slice(4) + '</p>'); document.writeln('<p>' + 'txt.substring(4) = ' + txt.substring(4) + '</p>'); document.writeln('<p>' + 'txt.substr(3, 3) = ' + txt.substr(3, 3) + '</p>'); document.writeln('<p>' + 'txt.slice(3, 3) = ' + txt.slice(3, 3) + '</p>'); document.writeln('<p>' + 'txt.substring(3, 3) = ' + txt.substring(3, 3) + '</p>'); document.writeln('<p>' + 'txt.slice(-8, -2) = ' + txt.slice(-8, -2) + '</p>'); document.writeln('<p>' + 'txt.slice(-2, -2) = ' + txt.slice(-2, -2) + '</p>'); document.writeln('<p>' + 'txt.substring(2, 30) = ' + txt.substring(2, 30) + '</p>'); //函数toLowerCase()用于把字符串转换为小写。 document.writeln('<p>' + 'txt.toLowerCase() = ' + txt.toLowerCase() + '</p>'); //函数toUpperCase()用于把字符串转换为大写。 document.writeln('<p>' + 'txt.toUpperCase() = ' + txt.toUpperCase() + '</p>');
总结整理
- 不要创建 String 对象。它会拖慢执行速度,并可能产生其他副作用
indexOf与search
- indexOf只能使用字符串检索,search即使用字符串检索,也可以使用正则表达式进行检索。
- indexOf可以指定开始检索的位置,search不能指定。
- indexOf(字符串)等价与search(字符串)。
substr、slice与substring
- substr第2个参数为截取的长度,slice、substring第2个参数为截取的最后前1位。
- slice的参数可以为负数或者正数,substring的参数只能是正数。