Number string

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 (如果isNaN函数的参数不是Number类型, 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); object

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

var str=String('1234'); 1234

str=String(true); true

str=String({x:1}); {obj   obj}

str=String(null); null

console.log(str);

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)); 空

charCodeAt():返回指定字符的ASCII码值

var str='abcdef';

console.log(str.charCodeAt(0)); 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)); ABC

concat():连接字符串

var str='hello ';

console.log(str.concat('world')); hello word

console.log(str.concat('world ','!'));  hello word !

indexOf();字符串搜索相关

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

A.localeCompare(b);比较两个字符串(比数值,如果前者大于后者则返回大于0的数,如果后者大于前者则返回小于0的数,相等则返回0)

console.log('h'.localeCompare('j')); -1

console.log('z'.localeCompare('a')); 1

console.log('a'.localeCompare('a')); 0

console.log('b'.localeCompare('B')); 1

RegExp 正则表达式

创建正则表达式的两种方法:

一是字面量、二是构造函数。

Demo

/ab+c/i;

new RegExp('ab+c', 'i');

new RegExp(/ab+c/, 'i');

正则表达式i表示大小写不敏感,g表示全局

match():找到一个或多个正则表达式的结果,返回一个数组

var str='this is a test of king show time';

var re=/IS/i;

console.log(str.match(re));

var str='QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioasdfghjkzxcvbnm';

console.log(str.match(/[a-f]/ig));

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

Replace();根据正则表达式进行替换

var str='this is a test';

var newStr=str.replace(/IS/ig,'!');

console.log(newStr); th!!a test

var str="2015-09-26";

var newStr=str.replace(/(\d{4})-(\d{2})-(\d{2})/,"$2/$3/$1");

console.log(newStr); 09/26/2015

var str="2015-09-25";

var newStr=str.replace(/(\d{4})-(\d{2})-(\d{2})/,func);

function func(match,d1,d2,d3){

return [d2,d3,d1].join('/');

}

console.log(newStr);

截取字符串slice(开始,结束);(slice左闭右开)substr(开始,长度);

var str='abcdef';

console.log(str.slice(2)); cdef

console.log(str.slice(0,2));    abc

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()将字符串拆分成数组split(x,y );按照x来进行拆分,y是长度

var str='2015-08-12';

var arr=str.split('-');

console.log(arr); 2015 08 12

var str='a b c d e f';

var arr=str.split(' ');

console.log(arr); a b c d e f

arr=str.split(' ',2);

console.log(arr); a b

字符串大小写相关

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');

产生链接

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 Number string的更多相关文章

  1. Eclipse的Jar包解压出System.js里String与Boolean定义分号可有可无吗?

    Eclipse的Jar包解压出System.js里String与Boolean定义分号可有可无吗? org.eclipse.wst.jsdt.core_1.3.300.v201410221502\li ...

  2. Js中String转int

    Js中String转int 方案一代码: Number(str) 方案二代码: //parseInt 方法都有两个参数, 第一个参数就是要转换的对象, 第二个参数是进制基数, 可以是 2, 8, 10 ...

  3. js实现String.Fomat

    引言 拼接字符串用习惯了C#的String.Format.今天看别人的代码在js中也封装了一个js的String.Format,用来拼接字符串和DOM. js实现和调用String.Format St ...

  4. JavaScript基础13——js的string对象

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. JS中String类型转换Date类型 并 计算时间差

    JS中String类型转换Date类型 1.比较常用的方法,但繁琐,参考如下:主要使用Date的构造方法:Date(int year , int month , int day)<script& ...

  6. perl malformed JSON string, neither tag, array, object, number, string or atom, at character offset

    [root@wx03 ~]# cat a17.pl use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' = ...

  7. hdu 4055 Number String(有点思维的DP)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. hdu4055 Number String

    Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. Number String

    Number String 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 dp 定义状态:dp[i][j]为当strlen=i,数字结尾为j的 ...

随机推荐

  1. php中excel以及cvs等导入以及导出

    一般网站后台都有人员导入或者是订单导出之类的操作,今天分享一下几种php excel cvs等文件导入导出的办法. 第一种比较简单的,自己写的,不引用任何excel类.但是会有bug,代码如下: 首先 ...

  2. react-native-router-flux

    这是一个路由,可以用来做Android底部的导航栏,学Android的都知道,如果用原生的代码来 做导航栏,会很复杂,关系到很多复杂的知识. 接下来我就简单的说明一下如何插入和使用吧: 1.你要先依赖 ...

  3. python修炼第三天

    今天主要讲了文件操作,函数与装饰器,装饰器比较烧脑,需要多做练习,逐步分解来进行理解!    加油! 一 文件操作 操作系统 提供文件的概念可以操作磁盘. 文件的只读模式: 注意如果是windows ...

  4. vue中父子组件的通信

    1.父组件向子组件传递数据 父组件传递:data = parent.data 子组件接收props: {data:{}} 2.子组件向父组件传递数据(https://vuefe.cn/v2/guide ...

  5. swiper使用中一些点的总结

    最近做了PC端改版,要求移动端有更好的体验,一些产品滚屏的展示,就用了swiper插件,以方便用户在移动端访问可以滑动翻屏展示. 本次项目中使用的是swiper2.0版本. 首先要引入swiper的j ...

  6. MinHook 分析01 (x86的jmp+offset类型hook)

    MinHook的原理就在于重写目标函数.在这次分析的X86模式中,32位相对JMP覆盖了整个地址空间.因为在相对地址计算中溢出的位被忽略,所以在X86模式中,函数的地址是容易掌控的. 直接来进入正题. ...

  7. c++ 指针、引用和取值;

    直接看代码: #include<iostream> using namespace std; int add(int *a,int *b){ int s; s = *a + *b; cou ...

  8. angular 定时函数

    注入$interval,$timeout   服务 2.定义函数 var aa = $interval(function(){ $timout(function(){ ..... }) },,定时时间 ...

  9. volatile 与 JVM 指令重排序

    前言: 在做单例模式时 有博客在评论区 推荐使用 volatile 关键字 进行修饰 然后用了两天时间查资料看文档 发现涉及的面太广 虽然已经了解为什么要使用 volatile + synchroni ...

  10. 零基础学习JavaSE(二)——基础语法

    二.Java 基础语法 2.1 Java 基础语法 java是一个面向对象的程序语言,及可把一切事物当做对象处理,而java的事物中最小的就是class (类),类中有方法,类可以创建对象,并且有一些 ...