javascript基础2
2015-07-15 16:48
741 查看
string类型
charAt() 和 charCodeAt()方法
1.1 sliece() , substr() , substring()
这三个方法都会返回操作字符串的一个子字符串
参数1:子字符串的起始位置
参数2(可选):表示子字符串到哪结束
slice() 和 substring() 的第二个参数:指定子字符串最后一个字符后面的位置
substr()的第二个参数:返回字符的个数
若没有设置参数2,则已字符串的长度作为结束点
1.2 字符串位置方法
indexOf() 与 lastIndexOf()
1.3 trim()方法
创建字符串副本,删除前置及后置的所有空格
1.4字符串大小写转换方法
1.5 字符串的模式匹配
match()
search()
1.6 localeCompare()方法
如果在字符串的字母表中应该排在字符串参数之前,则返回一个负数,一般为-1;
如果字符串等于字符串参数,返回0;
如果字符串在字母表中应该排在字符串参数之后,则返回一个正数,一般为1;
1.7 URI编码方法
encodeUR():用于整个URI(如 http://www.baidu.com/myhome.htm)
encodeURIComponent:用于URI某一段(如myhome.htm)
1.8 eval()方法
它只接收一个参数,即要执行字符串
2 Math对象
2.1min()和max()方法
确定一组数值中的最小(大)值
2.2 舍入方法
Math.ceil() 向上舍入
Math.floor() 向下舍入
Math.round() 四舍五入
2.3 random()方法
返回大于等于0小于1的一个随机数
charAt() 和 charCodeAt()方法
var stringValue="hello world"; console.log(stringValue.charAt(1)) //"e" 获取字符 console.log(stringValue.charCodeAt(1)) //输出"101" 字符编码
1.1 sliece() , substr() , substring()
这三个方法都会返回操作字符串的一个子字符串
参数1:子字符串的起始位置
参数2(可选):表示子字符串到哪结束
slice() 和 substring() 的第二个参数:指定子字符串最后一个字符后面的位置
substr()的第二个参数:返回字符的个数
若没有设置参数2,则已字符串的长度作为结束点
var stringValue="hello world"; console.log(stringValue.slice(3)); //"lo world" console.log(stringValue.substring(3)); //"lo world" console.log(stringValue.substr(3)); //"lo world" console.log(stringValue.slice(3,7)); //"lo w" 不含第7个字符 console.log(stringValue.substring(3,7)); //"lo w" 不含第7个字符 console.log(stringValue.substr(3,7)); //"lo worl" 长度为7 console.log(stringValue.slice(-3)); //"rld" console.log(stringValue.substring(-3)); //"hello world" console.log(stringValue.substr(-3)); //"rld" console.log(stringValue.slice(3,-4)); //"lo w" console.log(stringValue.substring(3,-4)); //"hel" console.log(stringValue.substr(3,-4)); //"" (空字符串) /* slice()和substr()传递一个负值参数时 它们的行为相同,-3将会转换为8(字符串长度 + 参数 11+(-3)=8) */ /* substring()则返回了全部字符串 将-3转换成0 */ /* 当第二个参数是负值是,这三个方法各不相同 slice方法将第二个参数转换为7,相当于 slice(3,7) substring方法会把第二个参数转换为0,相当于substring(3,0),但这个方法会将较大的数作为结束,较小的作为起始,最终为substring(0,3) substr方法会将第二个参数转换为0,意味着包含零个字符串,也就是空字符串 */
1.2 字符串位置方法
indexOf() 与 lastIndexOf()
/* 如果没有找到该字符串,则返回-1 */
var stringValue="hello world"; console.log(stringValue.indexOf("o")) //4 console.log(stringValue.lastIndexOf("o")) //7 /* 参数2表示从哪开始搜索 */ console.log(stringValue.indexOf("o",6)) //7 /* 通过循环匹配所有的字符串 */ var stringValue="Lorem ipsum dolor sit amet,consectetur adipisicing elit"; var positions=new Array(); var pos = stringValue.indexOf("e"); while(pos > -1){ positions.push(pos); pos=stringValue.indexOf("e",pos+1); } console.log(positions); //"3,24,32,35,52"
1.3 trim()方法
创建字符串副本,删除前置及后置的所有空格
var stringValue=" hello world "; var trimmedStringValue=stringValue.trim(); console.log(stringValue) //" hello world " console.log(trimmedStringValue) // "hello world"
1.4字符串大小写转换方法
var stringValue="hello world" console.log(stringValue.toLocaleUpperCase()); //"HELLO WORLD" 针对地区 console.log(stringValue.toUpperCase()); //"HELLO WORLD" console.log(stringValue.toLocaleLowerCase()); //"hello world" 针对地区 console.log(stringValue.toLowerCase()); //"hello world"
1.5 字符串的模式匹配
match()
var text="cat,bat,sat,fat" var pattern=/.at/; //与pattern.exec(text)相同 var matches=text.match(pattern); console.log(matches.index) //0 console.log(matches[0]); //"cat" console.log(pattern.lastIndex) //0
search()
/* 若没有找到匹配项 返回-1 */ var text="cat,bat,sat,fat" var pos=text.search(/at/); console.log(pos) //1 返回索引 /* 使用正则匹配所有想要更换的子字符串 */ var text="cat,bat,sat,fat"; var result=text.replace("at","ond"); console.log(result) //"cond,bat,sat,fat" result=text.replace(/at/g,"ond"); console.log(result); //"cond,bond,sond,fond"
1.6 localeCompare()方法
如果在字符串的字母表中应该排在字符串参数之前,则返回一个负数,一般为-1;
如果字符串等于字符串参数,返回0;
如果字符串在字母表中应该排在字符串参数之后,则返回一个正数,一般为1;
var stringValue="yellow"; console.log(stringValue.localeCompare("brick")); //1 console.log(stringValue.localeCompare("yellow")); //0 console.log(stringValue.localeCompare("zoo")); //-1
1.7 URI编码方法
encodeUR():用于整个URI(如 http://www.baidu.com/myhome.htm)
encodeURIComponent:用于URI某一段(如myhome.htm)
var uri="http://www.wrox.com/illegal value.htm#start"; /* "http://www.wrow.com/illegal%20value.htm#start" */ console.log(encodeURI(uri)) /* "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start" */ console.log(encodeURIComponent(uri))
1.8 eval()方法
它只接收一个参数,即要执行字符串
eval("console.log('hi')"); /* 等价于 */ console.log("hi")
2 Math对象
2.1min()和max()方法
确定一组数值中的最小(大)值
2.2 舍入方法
Math.ceil() 向上舍入
Math.floor() 向下舍入
Math.round() 四舍五入
2.3 random()方法
返回大于等于0小于1的一个随机数
/* 获取1-10的随机数 */ var num=Math.floor(Math.random()*10+1); /* 获取2-10的随机数 */ var num=Math.floor(Math.random()*9+2); /* 自由设定获取数值范围 */ function selectFrom(lowerValue,upperValue){ var choices=upperValue - lowerValue + 1; return Math.floor(Math.random()*choices + lowerValue); } var num=selectFrom(2,10) console.log(num) //介于2和10之间(包含2和10)的一个数值 /*例子*/ var colors=["red","green","blue","yellow","black","purple","brown"]; var color=colors[selectFrom(0,colors.length-1)]; console.log(color) //返回一个任意的颜色
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 异步流程控制:7 行代码学会 co 模块
- JavaScript拆分字符串时产生空字符的原因
- IE8开发人员工具教程(二)
- 在flex中执行一个javascript方法的简单方式
- Flex结合JavaScript读取本地路径的方法
- PowerShell中执行Javascript的方法示例
- javascript asp教程第六课-- response方法
- javascript asp教程More About Recordsets
- javascript asp教程第十二课---session对象
- javascript asp教程创建数据库连接
- javascript asp教程错误处理
- javascript asp教程第十课--global asa