美文网首页
javascript之string(字符串)

javascript之string(字符串)

作者: Jianshu9527 | 来源:发表于2017-03-25 19:45 被阅读31次
1 String是JavaScript基本数据类型之一
2 string类型是字符串的对象包装类型
3 string对象的方法可以在所以的基本字符串中进行访问

使用构造函数创建string

var stringValue = new string("hello string")

string类型的每个实例都有一个length属性

var stringValue = "hello world" 
alert(stringValue.length)  //11

注意!字符串中包含双字节符,每个字符也算是一个字符(长度属性-lengrh来计算字符串的长度)

string(方法)

1 字符方法( 用于访问字符串中特定字符的方法)

  • charAt()
  • charCodeAt()
  • [ ]
var stringValue = "hello world";
alert(stringValue.charAt(1))  //"e"
alert(stringValue[1]) //索引可以访问字符串的任意字符,并且索引是从零开始
//输出字符串中对应的字符
var stringValue = "hello world";
alert(stringValue.charCodeAt(1))  //"101"
//得到的是对应字符串字符编码值

2 字符串操作方法

  • concat() //字符串拼接
  • slice() //基于子字符串创建新字符串 方法
  • substr() //从起始索引号提取字符串中的指定数目的字符
  • substring() //提取字符串两个指定的索引号之间的字符
var stringValue = "hello";
var result = stringValue.concat("world");
alert(result)  //"hello world"
//concat()方法可以接受多个参数,之间可以用逗号隔开即可
//实际工作中使用加号操作符总归多一点("+") 自行取用即可
//        传入一个参数
var stringValue = "hello world";
alert(stringValue.slice(3))  //"lo world"  
alert(stringValue.substr(3))  //"lo world"
alert(stringValue.substring(3))  //"lo world"
//第一个参数指定字符串的开始位置
//        传入两个参数
var stringValue = "hello world";
alert(stringValue.slice(3,7))  //"lo w"  
alert(stringValue.substr(3,7))  //"lo w"
alert(stringValue.substring(3,7))  //"lo worl"(第二个参数表示的是返回的字符串个数)
//第二个参数表示字符串在那个位置结束
//注意!如果没有给第二个参数 则依据字符串的长度为结束点
//对原始的字符串没有任何影响
//      第一个参数为负数
var stringValue = "hello world";
alert(stringValue.slice(-3))  //"rld"  传入的值与字符串长度相加
alert(stringValue.substr(-3))  //"hello world"  负的第一个参数加上字符串的长度 负的第二个参数转换为0
alert(stringValue.substring(-3))  //"rld"  把所有的负值参数都转换成0
//      第二个参数为负数
var stringValue = "hello world";
alert(stringValue.slice(3,-4))  //"lo w"   会把第二个参数转换为7 相当于是slice(3,7)
alert(stringValue.substr(3,-4))  //"hel"  小的数作为开始 大的数作为结束 相当于是slice(0,3)
alert(stringValue.substring(3,-4))  //""(空字符串)  会将第二个参数转换为0 返回包含零个字符的字符串

3 字符串位置方法(字符串中查找子字符串)

  • indexOf()
  • lastIndexOf()//返回一个指定的字符串最后出现的位置,
//        第一个参数
var stringValue = "hello world";
alert(stringValue.indexOf("o"))  //4  从头开始向后搜索子字符串
alert(stringValue.lastIndexOf("o"))  //7  从尾开始向前搜索字符串(0表示最后出现的位置)
//        第二个参数
var stringValue = "hello world";
alert(stringValue.indexOf("o",6))  //7  
alert(stringValue.lastIndexOf("o",6))  //4  

4 字符串大小写转换方法

  • toLowerCase() //使用最多
  • toUpperCase() //使用最多
  • toLocaleLowerCase()
  • toLocaleUpperCase()
var stringValue = "hello world"
alert(stringValue.toLocaleUpperCase())  //"HELLO WORLD";
alert(stringValue.toUpperCase())  //"HELLO WORLD"
alert(stringValue.toLocaerCase())  //"hello world"
alert(stringValue.toLocaleLowerCase())  //"hello world"

5 字符查找

  • match('查找的字符') //查找特定的字符,找到则返回,不然则返回null

6 字符替换

  • replace('现有的指定字符','新的替换字符') //指定的字体串用于替换新指定的字符

7 字符串转为数组

  • split('指定的位置',返回的最大长度) //把字符串分割成字符串数组
var myString = 'a,b,c,d,e';
alert(myString.split('b'))  //a,  返回指定的字符前面的全部字符

8 valueOf() //返回字符对象的原始值

相关文章

网友评论

      本文标题:javascript之string(字符串)

      本文链接:https://www.haomeiwen.com/subject/enycottx.html