String

前言:javascript

  javascript没有表示单个字符的字符型,只有字符串String类型,字符型至关于仅包含一个字符的字符串。java

  字符串String是javascript基本数据类型,同时javascript也支持String对象,它是一个原始值的包装对象。正则表达式

  在须要时,javascript会自动在原始形式和对象形式之间转换。数组

一、定义:浏览器

  字符串String类型是由引号括起来的一组由16位Unicode字符组成的字符序列函数

二、特色:编码

  javascript中的字符串是不可变的。一旦字符串被建立,就永远没法改变它。url

  要改变某个变量保存的字符串,首先要销毁原来的字符串,而后再用另外一个包含新值的字符串填充该变量spa

能够经过+运算符链接其余字符串来建立一个新字符串code

var lang = "java";
lang = lang + "script"; //'javascript'

先建立一个可以容纳10个字符的新字符串,而后在这个字符串中填充'java'和'script',最后一步是销毁原来的字符串'java'和'script',由于这两个字符串已经没用

三、转字符串

四、属性

  字符串String类型的每一个实例都有一个length属性,表示字符串中的字符个数。因为字符串是不可变的,因此字符串的长度也不可变

  字符串的length属性不会在for/in循环中枚举,也不能经过delete操做符删除

对于字符串s来讲,最后一个字符的索引是s.length - 1

var str = "test";
console.log(str.length);//4
str.length = 6;
console.log(str,str.length);//"test",4

五、方法

【1】访问字符方法

【chartAt()】

接收一个基于0的字符位置的参数,返回指定位置的字符。当参数为空或NaN时,默认参数为0;当参数超出范围时,则返回一个空字符串

var str = "hello";
console.log(str.charAt(1));//e
console.log(str.charAt(-1));//''
console.log(str.charAt(10));//''
console.log(str.charAt());//h 
console.log(str.charAt(NaN));//h

  charAt()方法涉及到Number()函数的隐式类型转换若是转换为数值,则按照上述规则输出字符串;若是转换为NaN,则输出第0个字符

var str = "hello";
console.log(str.charAt(true));//'e'
console.log(str.charAt(false));//'h'
console.log(str.charAt('abc'));//'h'
console.log(str.charAt({}));//'h'
console.log(str.charAt([2]));//'l'

x.charAt(pos)与x.substring(pos, pos+1)、x.substr(pos,1)、x.slice(pos,pos+1)的结果相等  

var str = "hello";
console.log(str.charAt(1));//'e'
console.log(str.substring(1,2));//'e'
console.log(str.slice(1,2));//'e'
console.log(str.substr(1,1));//'e'

【charCodeAt()】

  接收一个基于0的字符位置的参数,但返回的是指定位置的字符16位Unicode编码

  返回值是一个16位的整数,在0-65535之间,即0x0000-0xffff之间。参数为空或NaN时,默认参数为0;当参数超出范围时,则返回NaN

var str = "hello";
console.log(str.charCodeAt());//104
console.log(str.charCodeAt(0));//104
console.log(str.charCodeAt(1));//101
console.log(str.charCodeAt(-1));//NaN
console.log(str.charCodeAt(10));//NaN
console.log(str.charCodeAt(NaN));//104

一样地,charCodeAt()方法涉及到Number()函数的隐式类型转换,若是转换为数值,则按照上述规则输出相应值;若是转换为NaN,则输出第0个字符的字符编码

var str = "hello";
console.log(str.charCodeAt(true));//101
console.log(str.charCodeAt(false));//104
console.log(str.charCodeAt('abc'));//104
console.log(str.charCodeAt({}));//104
console.log(str.charCodeAt([2]));//l08

【fromCharCode()】

  String构造函数自己有一个静态方法:fromCharCode()。这个方法的任务是接收一个或多个字符编码,而后把它们转换成一个字符串

  从本质上看,这个方法与实例方法charCodeAt()执行的是相反的操做。若参数为空或NaN时,则返回空字符串;若参数超出0-65535的范围,则输出字符不可控

console.log(String.fromCharCode(104,101,108,108,111));//'hello'
console.log(String.fromCharCode(0x6211,0x662f,0x5c0f,0x706b,0x67f4));//'我是小火柴'
console.log(String.fromCharCode());//''
console.log(String.fromCharCode(NaN));//''
console.log(String.fromCharCode(-1));
console.log(String.fromCharCode(65560));

//若是一个字符占用四字节,则须要拆成两个字符表示
console.log(String.fromCharCode(0xD842, 0xDFB7)); // "𠮷"

【[]】

  ECMAScript5定义了另外一个访问字符的方法,使用方括号加数字索引来访问字符串中的特定字符。

  若是参数超出范围或是NaN时,则输出undefined;没有参数时,会报错;

  该方法没有Number()转型函数的隐式类型转换,但参数为单数值数组时可转换为数值【IE7-浏览器不支持】

var str = "hello";
console.log(str[0]);//h
console.log(str[[1]]);//e
console.log(str[false]);//undefined
console.log(str[-1]);//undefined
console.log(str[NaN]);//undefined
console.log(str[]);//报错

【2】字符拼接

【concat()】

  将一个或多个字符串拼接起来,返回拼接获得的新字符串,而原字符串不发生改变

  若参数(第一个参数除外)不是字符串,则经过String()方法隐式转换为字符串,再进行字符串拼接

var stringValue = 'hello ';
var result = stringValue.concat('world','!');
console.log(result);//'hello world!'
console.log(stringValue);//'hello'

第一个参数只能是字符串,若是是其余类型(数组除外)则报错

(1).concat('2');//报错
(true).concat('false');//报错
({}).concat('abc');//报错

因为数组也存在concat()方法,参数会按照首先出现的参数是数组仍是字符串来决定如何转换

'1,2,3,'.concat([4,5]);//'1,2,3,4,5'
[1,2,3].concat(',4,5');//[1, 2, 3, ",4,5"]

【+】

var stringValue = 'hello ';
console.log(stringValue.concat('world','!'));//'hello world!'
console.log(stringValue + 'world' + '!');//'hello world!' 

当操做数其中一个是字符串,或者对象转换为字符串时,才进行字符串拼接

1 + 2;//3
'1' + 2;//'12'
var o = {valueOf:function(){return '1';}};
o + 2;//'12'
var o = {valueOf:function(){return 1;}};
o + 2;//3  

【3】建立子字符串

【slice()】

  slice(start,end)方法须要两个参数start和end,返回这个字符串中从start位置的字符到(但不包含)end位置的字符的一个子字符串。【对原字符串没有影响

若是end为undefined或不存在,则返回从start位置到字符串结尾的全部字符

若是start是负数,则start = max(length + start,0)

若是end是负数,则end = max(length + end,0)

var stringValue = 'hello world';
console.log(stringValue.slice());//'hello world'
console.log(stringValue.slice(2));//'llo world'
console.log(stringValue.slice(2,undefined));//'llo world'
console.log(stringValue.slice(2,-5));//'llo '
console.log(stringValue.slice(2,-20));//''
console.log(stringValue.slice(20));//''
console.log(stringValue.slice(-2,2));//''
console.log(stringValue.slice(-2,-20));//''  -----start和end没法交换位置          
console.log(stringValue.slice(-2,20));//'ld'
console.log(stringValue.slice(-20,2));//'he'
console.log(stringValue.slice(-20,-2));//'hello wor'

slice()方法涉及到Number()转型函数的隐式类型转换

当start被转换为NaN时,至关于start = 0;当end被转换为NaN时(end为undefined除外,至关于end=length),则输出空字符串

var stringValue = 'hello world';
console.log(stringValue.slice(NaN));//'hello world'
console.log(stringValue.slice(0,NaN));//''
console.log(stringValue.slice(true,[3]));//'el'
console.log(stringValue.slice(null,undefined));//'hello world'
console.log(stringValue.slice({}));//'hello world'
console.log(stringValue.slice('2',[5]));//'llo'

【substring()】

  substring(start,end)方法须要两个参数start和end,返回这个字符串中从start位置的字符到(但不包含)end位置的字符的一个子字符串。【对原字符串没有影响

若是end为undefined或不存在,则返回从start位置到字符串结尾的全部字符

若是任一参数是NaN或负数,则被0取代

若是任一参数大于字符串长度,则被字符串长度取代

若是start 大于 end,则交换它们的值

var stringValue = 'hello world';
console.log(stringValue.substring());//'hello world'
console.log(stringValue.substring(2));//'llo world'
console.log(stringValue.substring(2,undefined));//'llo world'
console.log(stringValue.substring(20));//''
console.log(stringValue.substring(-2,2));//'he'
console.log(stringValue.substring(NaN,2));//'he'
console.log(stringValue.substring(-2,20));//'hello world'
console.log(stringValue.substring(3,2));//'l'
console.log(stringValue.substring(3,NaN));//'hel'
console.log(stringValue.substring(-20,2));//'he'
console.log(stringValue.substring(-20,-2));//'' 

substring()方法也涉及到Number()转型函数的隐式类型转换

var stringValue = 'hello world';
console.log(stringValue.substring(true,[3]));//'el'
console.log(stringValue.substring(null,undefined));//'hello world'
console.log(stringValue.substring({}));//'hello world'
console.log(stringValue.substring('2',[5]));//'llo'

【substr()】-----【对原字符串没有影响】 

  substr(start,end)方法须要两个参数start和end,end表明返回的子字符串的字符个数;该方法返回这个字符串中从start位置的字符开始到end个字符的一个子字符串;

若是end为undefined或不存在,则返回从start位置到字符串结尾的全部字符

若是start是负数,则start = max(length + start,0)

若是start是NaN,则至关于start = 0

若是end是负数或NaN,则end = 0,所以会返回空字符串

start和end没法交换位置

  [注意]该方法不是ECMAScript标准,已经被弃用

  [注意]IE8-浏览器在处理向substr()传递负值的状况时存在问题,它会返回原始的字符串

var stringValue = 'hello world';
console.log(stringValue.substr());//'hello world'
console.log(stringValue.substr(2));//'llo world'
console.log(stringValue.substr(2,undefined));//'llo world'
console.log(stringValue.substr(2,NaN));//''
console.log(stringValue.substr(NaN,2));//'he'
console.log(stringValue.substr(20));//''
console.log(stringValue.substr(-2,3));//'ld'
console.log(stringValue.substr(-2,20));//'ld'
console.log(stringValue.substr(-20,2));//'he'
console.log(stringValue.substr(-20,-2));//''    
console.log(stringValue.substr(2,5));//llo w

  一样地,substr()方法也涉及到Number()转型函数的隐式类型转换

var stringValue = 'hello world';
console.log(stringValue.substr(true,[3]));//'el'
console.log(stringValue.substr(null,undefined));//'hello world'
console.log(stringValue.substr({}));//'hello world'
console.log(stringValue.substr('2',[5]));//'llo w'

 

对于以上三个建立子串的方法来讲,若是是空字符串,则不管参数是什么,仍然返回空字符串

var str = '';
console.log(str.slice(1));//''
console.log(str.substring(1));//''
console.log(str.substr(1));//''

【4】查找子字符串位置--------经过字符串查找位置

【indexOf()】

  indexOf(searchString,start)方法接收searchString和start两个参数,返回searchString首次出现的位置,若是没有找到则返回-1。

  searchString表示要搜索的子字符串;start表示该搜索的开始位置,若忽略该参数或该参数为undefined、NaN或负数时,start = 0

  该方法会隐式调用String()转型函数,将searchString非字符串值转换为字符串隐式调用Number()转型函数,将start非数字值(undefined除外)转换为数值

var string = 'hello world world';
console.log(string.indexOf('ld'));//9
console.log(string.indexOf('ld',undefined));//9
console.log(string.indexOf('ld',NaN));//9
console.log(string.indexOf('ld',-1));//9
console.log(string.indexOf('ld',10));//15
console.log(string.indexOf('ld',[10]));//15
console.log(string.indexOf('true',[10]));//-1
console.log(string.indexOf(false,[10]));//-1

【lastIndexOf()】

  与indexOf()不一样,lastIndexOf()从右向左查找。

  lastIndexOf(searchString,start)方法接收searchString和start两个参数,返回searchString第一次出现的位置,若是没有找到则返回-1

  searchString表示要搜索的子字符串;start表示该搜索的开始位置,若忽略该参数或该参数为undefined、NaN时,start = length - 1;若start为负数,start = 0

  一样地,该方法会隐式调用String()转型函数,将searchString非字符串值转换为字符串;隐式调用Number()转型函数,将start非数字值(undefined除外)转换为数值

var string = 'hello world world';
console.log(string.lastIndexOf('ld'));//15
console.log(string.lastIndexOf('ld',undefined));//15
console.log(string.lastIndexOf('ld',NaN));//15
console.log(string.lastIndexOf('ld',-1));//-1
console.log(string.lastIndexOf('h',-1));//0
console.log(string.lastIndexOf('w',undefined));//12

console.log(string.lastIndexOf('ld',10));//9
console.log(string.lastIndexOf('ld',[10]));//9
console.log(string.lastIndexOf('true',[10]));//-1
console.log(string.lastIndexOf(false,[10]));//-1

查找出字符串全部符合条件的子字符串

function allIndexOf(str,value){
    var result = [];
    var pos = str.indexOf(value);
    while(pos > -1){
        result.push(pos);
        pos = str.indexOf(value,pos+value.length);
    }
    return result;
}
console.log(allIndexOf('helllhelllhelll','ll'));//[2,7,12]

lastIndexOf()方法经常使用于获取URL地址中的扩展名

var url = "http://cnblogs.com/xiaohuochai.txt";
function getFileFormat(url){
    var pos = url.lastIndexOf('.');
    return url.slice(pos+1);
}
console.log(getFileFormat(url));//'txt'

【5】转化为数组

【split()】

  split()方法基于指定的分隔符将一个字符串分割成多个字符串,并将结果放在一个数组中,分隔符能够是字符串,也能够是一个RegExp

  该方法能够接受第二个参数(可选)用于指定数组的大小,若是第二个参数为0-array.length范围内的值时按照指定参数输出,其余状况将全部结果都输出

  若指定分隔符没有出如今字符串中,则以数组的形式返回原字符串的值

  [注意]参数中的正则表达式是否使用全局标志g对结果没有影响

var colorText = 'red,blue,green,yellow';
console.log(colorText.split(''));//["r", "e", "d", ",", "b", "l", "u", "e", ",", "g", "r", "e", "e", "n", ",", "y", "e", "l", "l", "o", "w"]
console.log(colorText.split(','));//["red", "blue", "green", "yellow"]
console.log(colorText.split(',',2));//["red", "blue"]
console.log(colorText.split(',',6));//["red", "blue", "green", "yellow"]
console.log(colorText.split('-'));//["red,blue,green,yellow"]
console.log(colorText.split(/\,/));//["red", "blue", "green", "yellow"]
console.log(colorText.split(/e/));//["r", "d,blu", ",gr", "", "n,y", "llow"]
console.log(colorText.split(/[^\,]+/));//将除去逗号之外的字符串变为分隔符["", ",", ",", ",", ""],IE8-会识别为[",",",",","]