-
JS String对象
String 对象属性 | |
属性 | 描述 |
constructor | 对创建该对象的函数的引用 |
length | 字符串的长度 |
prototype | 允许您向对象添加属性和方法 |
string 对象方法 | ||
方法 | 描述 | 用法 |
字符串样式 | ||
用于创建 HTML 锚。 |
var txt="Hello world!" document.write(txt.anchor("myanchor")); // 上面的代码将输出为纯粹的 HTML:<a name="myanchor">Hello world!</a> |
|
big() | 返回在指定的位置的字符的 Unicode 编码。 |
str.big()
|
small() | 使用小字号来显示字符串。 |
str.small()
|
bold() | 使用粗体显示字符串。 |
str.bold()
|
italics() | 使用斜体显示字符串。 |
str.italics()
|
blink() | 显示闪动字符串。 |
str.blink()
|
fixed() | 以打字机文本显示字符串。 |
str.fixed()
|
strike() | 使用删除线来显示字符串。 |
str.strike()
|
fontcolor() | 使用指定的颜色来显示字符串。 |
str.fontcolor("Red")
|
fontsize() | 使用指定的尺寸来显示字符串。 |
str.fontsize(7)
|
toLowerCase() | 将整个字符串转成小写字母。 |
str.toLowerCase()
|
toUpperCase() | 将整个字符串转成大写字母。 |
str.toUpperCase()
|
toLocaleLowerCase() | 把字符串转换为小写。 |
str.toLocaleLowerCase()
|
toLocaleUpperCase() | 把字符串转换为大写。 |
str.toLocaleUpperCase()
|
sub() | 把字符串显示为下标。 |
str.sub()
|
sup() | 把字符串显示为上标。 |
str.sup()
|
link() | 将字符串显示为链接。 |
document.write(
|
字符串操作 |
||
concat() | 将两个或多个字符的文本组合起来,返回一个新的字符串。 | str.concat(str2,str3...) |
indexOf() | 返回字符串中一个子串第一处出现的索引(从左到右搜索)。如果没有匹配项,返回 -1 。 |
var str="Hello world!" str.indexOf("Hello"); // 返回 0 str.indexOf("World",0); // 从0位置找World,返回-1 |
charAt() | 返回指定位置的字符。 | str.charAt(1) |
lastIndexOf() | 返回字符串中一个子串最后一处出现的索引(从右到左搜索),如果没有匹配项,返回 -1 。 |
var str="Hello world!" str.lastIndexOf("world") // 最后一个 world 出现的索引 str.lastIndexOf("world",0) // 从指定位置查找 world 字符串 |
match() | 检查一个字符串匹配一个正则表达式内容,如果么有匹配返回 null。 |
var str="1 plus 2 equal 3!" str.match("plus "); // 检索一个字符串。 str.match(/d+/g); // 检索一个正则表达式的匹配。 |
substring() | 返回字符串的一个子串,传入参数是起始位置和结束位置。 |
var str="Hello world!" str.substring(3); // 从指定位置到最后,返回lo world! str.substring(3,7); // 从3到7,返回lo w |
substr() | 返回字符串的一个子串,传入参数是起始位置和长度 |
var str="Hello world!" str.substr(3); // 从3截取到最后 str.substr(3,7); // 从3开始截取7位 str.substr(-4); // -1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。 |
replace() | 用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。 |
var str="Visit Microsoft!" str.replace(/Microsoft/, "W3School"); // 用 "W3School" 替换字符串中的 "Microsoft" str.replace(/Microsoft/g, "W3School"); // 全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3School" str.replace(MIcrosoft/i, "Microsoft"); // 对大小写不敏感的搜索替换 name = "Doe, John"; name.replace(/(w+)s*, s*(w+)/, "$2 $1"); // 转换姓名的格式,把 "Doe, John" 转换为 "John Doe" name = '"a", "b"'; name.replace(/"([^"]*)"/g, "'$1'"); // 把所有的花引号替换为直引号 // 把字符串中所有单词的首字母都转换为大写: name = 'aaa bbb ccc'; uw=name.replace(/w+/g, function(word) { return word.substring(0,1).toUpperCase()+word.substring(1);} ); |
search() | 执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。 |
var str="Visit W3School!" str.search(/W3School/); // 检索 "W3School",返回6 str.search(/w3school/); // search() 对大小写敏感,无法检索到,返回-1 str.search(/w3school/i); // 忽略大小写的检索 |
slice() | 提取字符串的一部分,并返回一个新字符串(与 substring 相同)。 |
// String 对象的方法 slice()、substring() 和 substr() (不建议使用)都可返回字符串的指定部分。slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。slice() 与 substr() 有所不同,因为它用两个字符的位置来指定子串,而 substr() 则用字符位置和长度来指定子串。 var str="Hello happy world!" str.slice(6); // 提取从位置 6 开始的所有字符 str.slice(6,11); // 提取从位置 6 到位置 11 的所有字符 |
split() | 通过将字符串划分成子串,将一个字符串做成一个字符串数组。 |
var str="How are you doing today?" str.split(" "); // 输出 How,are,you,doing,today? str.split(""); // 输出 H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? str.split(" ",3); // 输出 How,are,you,指定返回的数组的最大长度为3 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 | str.charCodeAt(1); |
fromCharCode() | 从字符编码创建一个字符串。 |
document.write(String.fromCharCode(72,69,76,76,79)); // 输出 HELLO |
localeCompare() | 用本地特定的顺序来比较两个字符串。 |
-
JS 对象与字符串的互相转换
1:jQuery插件支持的转换方式
$.parseJSON(str); // 可以将json字符串转换成json对象
2:浏览器支持的转换方式(Firefox,chrome,opera,safari,ie9,ie8)等浏览器代码如下:
JSON.parse(str); // 将 json 字符串转换成 json 对象
JSON.stringify(json); // 将 json 对象转换成 json 字符串
注:ie8(兼容模式),ie7和ie6没有JSON对象,需要引入 json.js 或 json2.js。
下载地址: https://github.com/douglascrockford/JSON-js
3:Javascript支持的转换方式
eval('(' + jsonstr + ')'); // 可以将json字符串转换成json对象,要在json字符外包裹一对小括号
注:ie8(兼容模式),ie7和ie6也可以使用eval()将字符串转为JSON对象,但不推荐这些方式,这种方式不安全eval会执行json串中的表达式。
-
JS Array 对象:
Array 对象属性 | |
---|---|
属性 | 描述 |
constructor | 返回对创建此对象的数组函数的引用。 |
length | 设置或返回数组中元素的数目。 |
prototype | 使您有能力向对象添加属性和方法。 |
Array 对象方法 | |
---|---|
方法 | 描述 |
concat() | 连接两个或更多的数组,并返回结果。 |
join() | 把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。 |
pop() | 删除并返回数组的最后一个元素 |
push() | 向数组的末尾添加一个或更多元素,并返回新的长度。 |
reverse() | 颠倒数组中元素的顺序。 |
shift() | 删除并返回数组的第一个元素 |
slice() | 从某个已有的数组返回选定的元素 |
sort() | 对数组的元素进行排序 |
splice() | 删除元素,并向数组添加新元素。 |
toSource() | 返回该对象的源代码。 |
toString() | 把数组转换为字符串,并返回结果。 |
toLocaleString() | 把数组转换为本地数组,并返回结果。 |
unshift() | 向数组的开头添加一个或更多元素,并返回新的长度。 |
valueOf() | 返回数组对象的原始值 |