js特殊的引用类型- Number 和 String常用方法

928 阅读3分钟

一、Number类型

(1)创建

Number 是与数字值对应的引用类型。要创建 Number 对象,可以在调用 Number 构造函数时向其 中传递相应的数值。

 var numberObject = new Number(10);

(2) toString()方法

可以用 toString()方法传递一个表示基数的参数,告诉它返回几进制 数值的字符串形式

var num = 10;
console.log(num.toString()); //"10"
console.log(num.toString(2)); //"1010"
console.log(num.toString(8)); //"12"
console.log(num.toString(10)); //"10"
console.log(num.toString(16)); //"a

(3)toFixed()方法

var numOne = 10;
alert(numOne.toFixed(2)); //"10.00"
var numTwo = 10.005;
alert(numTwo.toFixed(2)); //"10.01"

Number 对象通常以后台方式为数值提供了重要的功能。但与此同时,我们仍 然不建议直接实例化 Number 类型。

二、String类型

(1)创建

String 类型是字符串的对象包装类型,可以像下面这样使用 String 构造函数来创建。

 var stringObject = new String("hello world");

(2)字符方法 charAt(),charCodeAt(),fromCharCode()

  • charAt() 返回在指定位置的字符。
  • charCodeAt() 返回在指定的位置的字符的 Unicode 编码
  • fromCharCode() 将 Unicode 编码转为字符
 var str= 'hello world!'//charAt() 返回在指定位置的字符。
str.charAt(1);//e

//charCodeAt() 返回在指定的位置的字符的 Unicode 编码。
str.charCodeAt(1);//101

//fromCharCode() 将 Unicode 编码转为字符。
String.fromCharCode(104,101,108,108,111); //"hello"

(3)字符串操作方法 concat(),slice(),substr(),substring()

  • concat() 连接两个或更多字符串,并返回新的字符串
  • slice() 提取字符串的片断,并在新的字符串中返回被提取的部分
  • substring() 提取字符串中两个指定的索引号之间的字符(与slice类似)
  • substr() 从起始索引号提取字符串中指定数目的字符
 var str= 'hello';

//1.concat() 连接两个或更多字符串,并返回新的字符串
var result = str.concat(" world!") ;//"hello world!"

//2.slice() 提取字符串的片断,并在新的字符串中返回被提取的部分
result.slice(3); //"lo world!"
result.slice(3,7); //"lo w"(7代表索引位置)

//3.substring() 提取字符串中两个指定的索引号之间的字符(与slice类似)。
result.substring(3);//"lo world!"
result.substring(3,7);//"lo w"

//4.substr() 从起始索引号提取字符串中指定数目的字符。
result.substr(3);//"lo world!"
result.substr(3,7);//"lo worl"(7代表个数)

(4)字符串位置方法 indexOf(),lastIndexOf()

  • indexOf() 从前往后找 返回某个指定的字符串值在字符串中首次出现的位置
  • lastIndexOf() 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置
var str = "hello world!";

//indexOf()  从前往后找 返回某个指定的字符串值在字符串中首次出现的位置
str.indexOf("o");//4
str.indexOf("o",6);//7

//lastIndexOf() 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。
str.lastIndexOf("o");//7
str.lastIndexOf("o",6);//4

(5)trim()方法

去除字符串两边的空白

  var str =" hello ";
  str.trim();//"hello"

(6)字符串大小写转换方法 toUpperCase(),toLowerCase()

  • toUpperCase() 把字符串转换为大写
  • toLowerCase() 把字符串转换为小写
  var str = "hello world!";
  str.toUpperCase();//"HELLO WORLD!"
  str.toLowerCase();//"hello world!"

(7)字符串模式匹配方法 search(),replace(),match(),split()

  • search() 查找与正则表达式相匹配的值

  • split() 把字符串分割为字符串数组

  • match() 查找找到一个或多个正则表达式的匹配

  • replace() 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串

var text = "cat, bat, sat, fat"

//search()  查找与正则表达式相匹配的值。
text.search(/at/);//1

//split()   把字符串分割为字符串数组。
result.split();//["hello world!"]

//实用方法,字符串反转
result.split("").reverse().join(""); //"!dlrow olleh"

//match()   查找找到一个或多个正则表达式的匹配。
text.match(/c/);//["c", index: 0, input: "cat, bat, sat, fat", groups: undefined]

//replace() 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串。
text.replace("at","ond");//"cond, bat, sat, fat"
text.replace(/at/g,"ond");//"cond, bond, sond, fond"

(8)valueOf()方法

valueOf() 返回某个字符串对象的原始值。

  var text = "cat, bat, sat, fat";
  text.valueOf();//"cat, bat, sat, fat"