JavaScript字符串方法

一、创建字符串
  1. 创建字符串的三种办法: new String(), String(), 直接量,三种方式可以创建。
  2. String即文本(字符串),字符串方法都不改原字符串;
  3. length可读不可写。
var str = new String('hello');
var str = String('hello'); //内建函数
var str = 'hello'; //字面量

console.log(str.length);
str.length = 10;
console.log(str.length);
复制代码
二、常用字符串方法
  1. toLowerCase:转小写、toUpperCase:转大写。
var str = 'Abcde';
console.log(str.toUpperCase()); //ABCDE
console.log(str.toLowerCase()); //abcde
复制代码
  1. indexOf:查找字符串,返回索引值。查找当前字符在字符串中的从左到右第一次出现的位置,如果没有找到就返回-1。第二个参数表示从字符串中的哪个位置(索引值)开始搜索,也就是说indexOf()会从该参数指定的位置向后搜索,忽略该位置之前的所有字符,做判断要用>-1lastIndexOf:查找当前字符串这个的从右往左第一次出现的位置,如果没有找到就返回-1。第二个参数则会从指定的位置向前搜索,忽略该位置之后的所有字符。
var str = "hello world";
if(str.indexOf("h") > -1){
    console.log("能找到");
}else{
    console.log("不能找到");
}
alert(str.indexOf("o", 6)); //7 indexOf()是从位置6(字母“w”)开始向后搜索
alert(str.lastIndexOf("o", 6));  //4
复制代码
  1. substringsubstrslice。这三个方法都会返回被操作字符串的一个子字符串,都接受一或两个参数。第一个参数指定子字符串的开始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束。其中slicesubstring的第二个参数指定的是子字符串最后一个字符后面的位置(左闭右开[))。而substr的第二个参数指定的则是返回的字符个数。如果没有传递第二个参数,则将字符串的末尾作为结束位置。
var str = "hello world";
alert(str.slice(3)); //"lo world"
alert(str.substring(3)); //"lo world"
alert(str.substr(3)); //"lo world"
alert(str.slice(3, 7)); //"lo w"
alert(str.substring(3, 7)); //"lo w"
alert(str.substr(3, 7)); //"lo worl"
复制代码

如果传递的参数是负数,slice会将传入的负值与字符串的长度相加。substr将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0。substring会将所有的负值都转换成0。

var str = "hello world";
alert(str.slice(-3)); //"rld"
alert(str.substring(-3)); //"hello world"
alert(str.substr(-3)); //"rld"
alert(str.slice(3, -4)); //"lo w"
alert(str.substring(3, -4)); //"hel" 将第二个参数转换为0,使调用变成了substring(3, 0),而这个方法会将较小的数作为开始位置,将较大的数作为结束位置,因此相当于调用substring(0, 3)
alert(str.substr(3, -4)); //""(空字符串)
复制代码
  1. split:将字符串按照指定的分割符分割成数组,第一个参数为标志符,第二个参数为切割个数。
var str = 'abcdefg';
var str1 = 'hello world how are you';
console.log(str1.split(' ',2));
复制代码
  1. trim:清空两侧空格。
var str = ' 你好 ';
console.log(str.trim());
复制代码
  1. match:查找指定的值,返回匹配的值,未找到返回null,正则可以找一个或多个表达式。
var  str = 'hello world';
str.match('o')//字符串 
str.match(/o/i)//正则 
复制代码
  1. search:返回检索字符串首次出现的位置,未找到返回-1。
var  str = 'hello world';
 str.search('o'); //4
复制代码
  1. replace:用一些字符替换另一些字符,第二个参数可以是字符串,也可以是函数。
var  str = 'hello world';
str.replace('o','a'); //hella world

var  str = 'hello world';
function fn(){
    return 'a';
}
var  call = str.replace(/o/g, fn);
console.log(call);//hella warld
复制代码
三、不常用字符串方法
  1. charAt:在哪个字符,参数是索引值。
var str = 'abcdefg';
console.log(str[1]); //b
console.log(str.charAt(1)); //b
复制代码
  1. concat:连接两个/多个字符串,性能极其低下。
var str = 'abcdefg';
var str2 = '123';
console.log(str.concat(str2)); //abcdefg123
复制代码
  1. charCodeAt:字符编码,是Unicode编码,参数是索引值。
var str = 'abcdefg';
console.log(str.charCodeAt(0)); //97
复制代码
  1. fromCharCode:来自某一个字符编码
console.log(String.fromCharCode(97)); //a
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值