标签:style blog io ar color sp java on div
javascript-String对象
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>String对象</title>
<script>
// 字符串常用的方法
// 1.charAt() 返回在指定位置的字符
console.log(‘=========charAt()=========‘);
var abc = ‘telephone‘;
console.log(abc.charAt(4)); // p
// 2.concat() 连接字符串 (Array对象也有此方法用于两个数组的连接)
console.log(‘=========concat()=========‘);
var a1 = ‘my‘;
var a2 = ‘name‘;
console.log( a1.concat(a2) ); // myname
// 3.indexOf(searchvalue,fromindex) 检索字符串 返回某个指定的字符串值在字符串中首次出现的位置
// fromindex索引值为:0~字符串长度-1
// 如果没找到则返回-1
var b1 = ‘detection‘;
console.log(‘=========indexOf()=========‘);
console.log( b1.indexOf(‘a‘) ); //-1
console.log( b1.indexOf(‘e‘) ); //1
// 4.lastIndexOf() 同indexOf() 向后向前检索,返回该字符在字符串的索引
console.log(‘=========lastIndexOf()=========‘);
console.log( b1.lastIndexOf(‘e‘) ); //3
// 5.match(searchvalue|regexp) 找到一个或多个正则表达式的匹配。返回的是值
console.log(‘=========match()=========‘);
var sMatch = ‘hello world‘;
console.log( sMatch.match(‘l‘) ); //["l", index: 2, input: "hello world"]
console.log( sMatch.match(/l/) ); //["l", index: 2, input: "hello world"]
console.log( sMatch.match(/l/g) ); //["l", "l", "l"] 全局匹配
// 6.replace() 替换与正则表达式匹配的子串
console.log(‘=========replace()=========‘);
var sReplace = ‘tony#163.com‘;
console.log( sReplace.replace(‘#‘, ‘@‘) );
console.log( sReplace.replace(/\d+/, ‘789‘) );
console.log( sReplace.replace(/(\w+)#(\w+)/, "$2#$1") );
// 7.search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
// 返回值 stringObject 中第一个与 regexp 相匹配的子串的起始位置。
// 如果没有找到返回-1
console.log(‘=========search()=========‘);
var sSearch = ‘l, index: 2, input: hello world‘;
console.log( sSearch.search(‘2‘) );
console.log( sSearch.search(‘k‘) );
// 8.slice(start,end) 提取字符串的片断,并在新的字符串中返回被提取的部分。
// 英 [sla?s] vt.切成片; 切下; 划分
console.log(‘=========slice()=========‘);
var sSlice = ‘tony@163.com‘;
console.log( sSlice.slice(5) ); //163.com
console.log( sSlice.slice(4, 6) ); //@1
// 9.split(separator,howmany) 方法用于把一个字符串分割成字符串数组。
// 英 [spl?t] vt.分裂; 分开; <俚>(迅速)离开; 分担
// 参数:分割符,长度
// 返回值:数组
// String.split() 执行的操作与 Array.join 执行的操作是相反的。
console.log(‘=========split()=========‘);
var sSplit = ‘jack+ jane+ jay+ tony‘;
var s = sSplit.split(‘+‘);
console.log(s instanceof Array); //true
console.log(s);
console.log( sSplit.split(‘+‘, 2) );
// 10.substr(start,length) 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
console.log(‘=========substr()=========‘);
var sSubstr = ‘Hello world!‘;
console.log( sSubstr.substr(2) ); //llo world!
console.log( sSubstr.substr(2, 7) ); //llo wor
// 11.substring(start,stop) 方法用于提取字符串中介于两个指定下标之间的字符。
// 参数为 一个非负的整数
console.log(‘=========substring()=========‘);
var sSubstring = ‘Hello world!‘;
console.log( sSubstring.substring(2) ); //llo world!
console.log( sSubstring.substring(2, 7) ); //llo w
</script>
</head>
<body>
</body>
</html>
标签:style blog io ar color sp java on div
原文地址:http://www.cnblogs.com/lostyu/p/4139109.html