js string和number
number
Js只有一种数字类型(包括整型,浮点型)
极大或极小的可用科学计数法来表示。(7.7123e+1)
所有js数字均为64位
Js所有的数字都存储为浮点型
小数的最大位数是17位
0开头的为八进制 0x开头的为16进制
console.log(Number.MAX_VALUE); 最大值
console.log(Number.MIN_VALUE);最小值
console.log(Number.NEGATIVE_INFINITY);极大值
console.log(Number.POSITIVE_INFINITY);极小值
IsNaN 判断是不是NaN
console.log(Number.isNaN(NaN)); true
console.log(Number.isNaN(Number.NaN)); true
console.log(Number.isNaN(0/0)); true
console.log(Number.isNaN('NaN')); false
console.log(Number.isNaN('')); false
console.log(Number.isNaN('123')); false
console.log(Number.isNaN(true)); false
console.log(Number.isNaN(undefined)); false
console.log(Number.isNaN(' ')); false
toFixed();四舍五入为指定小数位数的数字
var n=12345.6789;
console.log(n.toFixed()); 12346
console.log(n.toFixed(1)); 12345.7
console.log(n.toFixed(2)); 12345.68
console.log(n.toFixed(6)); 12345.678900
console.log(1.23e+20.toFixed(2)); 123000000000000000000.00
console.log(1.23e-20.toFixed(2)); 0.00
console.log(2.45.toFixed(1)); 2.5
toExponential(x);把对象的值转变为指数计数法,x规定小数位数
var n=77.1234;
console.log(n.toExponential()); 7.71234e+1
console.log(n.toExponential(2)); 7.71e+1
console.log(n.toExponential(4)); 7.7123e+1
Toprecision();对象的值超出指定位数时将其转换为指数计数法;
var n=5.1234567;
console.log(n.toPrecision()); 5.1234567
console.log(n.toPrecision(1)); 5
console.log(n.toPrecision(2)); 5.1
console.log(n.toPrecision(5)); 5.1235
console.log((1234.567).toPrecision(2)); 1.2e+3
String对象
var str='king';
console.log(typeof str); //string
var strObj=new String('king');
console.log(typeof strObj); //obj
console.log(strObj[0]); //k
console.log(strObj.length); //4
console.log(strObj.valueOf()); //king
console.log(strObj.toString()); //king
console.log("nana"[0]); //n
console.log("helloworld".length); //10
//charAt()根据下标返回指定的字符
var str='king';
console.log(str.charAt(0)); //k
console.log(str.charAt(1)); //i
console.log(str.charAt(2)); //n
console.log(str.charAt(3)); //g
console.log(str.charAt(10)); //
console.log(str.charAt(b)); //-1
//charCodeAt():返回指定字符的ASCII码值
var str='abcdef';
console.log(str.charCodeAt(0));//a的ASCII码值97
console.log(str.charCodeAt(100)); 空的值为 NaN
console.log(str.charCodeAt(-123)); NaN
//fromCharCode():根据指定的ASCII放回对应的字符
console.log(String.fromCharCode(97)); //a
console.log(String.fromCharCode(65,66,67));
var str='hello ';
console.log(str.concat('world'));
// hello world
console.log(str.concat('world ','!'));
// hello world!
//字符串搜索相关
var str='this is a test';
var str='this is a test';
console.log(str.indexOf('t')); 0
console.log(str.indexOf('is')); 2
console.log(str.indexOf('Is')); -1
console.log(str.indexOf('i')); 2
console.log(str.indexOf('i',3)); 5//此处的3指的是从第三位开始
通过indexOf()可以统计一个字符在指定字符串中出现的次数
var str='sssssdlkfjlwk34jlksdfjlksjdlf234';
var count=0;
var pos=str.indexOf('s');
while(pos!=-1){
count++;
pos=str.indexOf('s',pos+1);
}
console.log(count);
lastIndexOf():最后一次出现的位置
var str='this is a test';
console.log(str.indexOf('is'));
2
console.log(str.lastIndexOf('is'));
5
比较两个字符串
console.log('h'.localeCompare('j'));
104和106
大 显示负数
后面小 显示正数
match():找到一个或多个正则表达式的结果
var str='this is a test of
king show time';
var
re=/IS/i; i不区分大小写
console.log(str.match(re));
var str='QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioasdfghjkzxcvbnm';
console.log(str.match(/[a-f]/ig));
g是全局匹配(搜索)
search():根据正则表达式进行搜索
var str='this is a test';
console.log(str.search(/is/));
2
console.log(str.search(/IS/));
-1
console.log(str.search(/IS/i));
2
var str='this is a test';
var
newStr=str.replace(/IS/ig,'!'); th! ! a test
把is替换成!
var str="2015-09-26";
var
newStr=str.replace(/(\d{4})-(\d{2})-(\d{2})/,"$2/$3/$1");
09/26/2015 换位置
截取字符串
var str='abcdef';
console.log(str.slice(2)); cdef
console.log(str.slice(0,2)); 左开右闭
ab 只显示0 1
后面闭合相当于没有
console.log(str.slice(-3));
def
console.log(str.slice(-4,-2));
cd
console.log(str.slice(0,-1));
abcde
console.log(str.substr(3));
def
console.log(str.substr(0,4));
abcd
split()将字符串拆分成数组
var
str='2015-08-12';
var
arr=str.split('-'); ["2015", "08",
"12"]
console.log(arr);
var
str='a b c d e f';
var
arr=str.split(' ');
["a", "b", "c", "d",
"e", "f"]
console.log(arr);
arr=str.split('
',2);
["a", "b"]
console.log(arr);
字符串大小写相关
(大写换小写 小写换大写)
console.log("KING".toLowerCase());
console.log("KING".toLocaleLowerCase());
console.log('nana'.toUpperCase());
console.log('nana'.toLocaleUpperCase());
trim()
var str=' abc ';
alert("!"+str+"!");
有空格
alert("!"+str.trim()+"!");
没有空格
产生锚点
var
str="this is a test";
document.body.innerHTML=str.anchor('contents_anchor');
this is a test 把变量this is a test输入进str.anchor('contents_anchor');
括号里面
产生链接
var title='this is of king
show time';
var
url='http://phpfamily.org';
document.write('Click
Me to Visit My Blog'+title.link(url)); 把链接代替到英文字母里 点字母跳入到链接里

js string和number的更多相关文章
- IE6,IE7,IE8下报JS错误:expected identifier, string or number的原因及解决的方法
今天在调试一个页面的时候遇到一个问题,在IE9下执行得非常好的脚本,在IE8里打开的时候弹出错误:expected identifier, string or number,依照经验,应该是定义对象的 ...
- JavaScript在IE6,IE7下报错'expected identifier, string or number'
问题: 代码在Forefox和IE8下工作正常,但是在IE6下报错: expected identifier, string or number 假如变量options有多个选项,那么我们可以用逗号分 ...
- js中声明Number的五种方式
转载自:http://www.jb51.net/article/34191.htm <!DOCTYPE html> <html> <head> <meta c ...
- ie6 ie7下报脚本错误"Expected identifier, string or number" 的原因和解决方法
在IE6和ie7里面,脚本报错"Expected identifier, string or number" 写下这个是个之前我已经很头疼了,因为我的代码在其他浏览器里都是正常的, ...
- err Invalid input of type: 'dict'. Convert to a byte, string or number first
一个问题引发的血案: 用python向redis写入数据报错: redis.exceptions.DataError: Invalid input of type: 'dict'. Convert t ...
- js 中的 number 为何很怪异
js 中的 number 为何很怪异 声明:需要读者对二进制有一定的了解 对于 JavaScript 开发者来说,或多或少都遇到过 js 在处理数字上的奇怪现象,比如: > 0.1 + 0.2 ...
- js String方法总结
字符方法(3) charAt(pos: number): string; // 返回特定位置的字符. charCodeAt(index: number): number; // 返回表示给定索引的字符 ...
- Right in the Center (js string algorithm)
Right in the Center (js string algorithm) codewars https://www.codewars.com/kata/5f5da7a415fbdc0001a ...
- javax.script.ScriptException: ReferenceError: "window" is not defined in security.js at line number 10
使用jmeter执行加密登录接口的测试遇到的问题. 问题记录: 今天使用jmeter执行加密登录接口的测试,因为测试环境的应用包是以前的老版本(可能有两年了),所以需要替换加密文件:security. ...
随机推荐
- mysql添加外键无法成功的原因
最近很忙,碰到很多问题都忘了发上来做个记录,现在又忘了,FUCK,现在碰到一个问题, 就是mysql添加外键总是无法成功,我什么都试了,就是没注意signed和unsigned,FUCK,因为我用my ...
- JS常见兼容性问题
兼容性问题:函数(方法)兼容 描述:部分W3C指定的函数,有部分老的浏览器不支持 解决: 条件判断,如果有,则使用,添加原型方法,例如 String 的 trim 方法 if(!String.prot ...
- 深度学习实践-强化学习-bird游戏 1.np.stack(表示进行拼接操作) 2.cv2.resize(进行图像的压缩操作) 3.cv2.cvtColor(进行图片颜色的转换) 4.cv2.threshold(进行图片的二值化操作) 5.random.sample(样本的随机抽取)
1. np.stack((x_t, x_t, x_t, x_t), axis=2) 将图片进行串接的操作,使得图片的维度为[80, 80, 4] 参数说明: (x_t, x_t, x_t, x_t) ...
- Python module ---- re
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工具.python的re模块,在绝大 ...
- 6993: Dominoes(纯bfs)
题目描述Orz likes to play dominoes. Now giving an n*m chessboard and k dominoes whose size are 1*2, Orz ...
- es 服务器搭建
安装jdk,原系统安装的openjava 参考https://www.cnblogs.com/Dylansuns/p/6974272.html注意配置/etc/profile 时,要注意自己安装的是哪 ...
- javascript中的map和reduce
今天在看“廖雪峰官方网站”的js教程时,看到了map和reduce.其中有一个练习题是:不使用js内置的parseInt()函数,利用map和reduce操作实现一个string2int()函数(先将 ...
- cpu资源长期使用率过高导致系统内核锁问题
服务器跑大量高负载程序,会造成cpu soft lockup. 解决办法: #追加到配置文件中 echo 30 > /proc/sys/kernel/watchdog_thresh #查看 [r ...
- Django 自带登录验证:authenticate和login,login_require,logout模块
验证之前需要在settings 中指定验证数据models AUTH_USER_MODEL = 'crm.UserProfile'#app名字.表名字 1.authenticate是一个方法,验证账号 ...
- Hbase--知识点总结3
Hbase知识点总结: hbase表中为什么列族的数量不能太多? 因为当一个列族数据溢写的时候,其他列族也会发生数据溢写,但是其他列族中数据的数量还没有达到溢写的阈值,就会导致产生的小文件数量增多. ...