JavaScript之String的常用方法

String对象的方法

var str = 'this is a str1';  //字符串变量
var str = new String('this is a str2');  //字符串对象
这是定义String 类型 变量 的两种方式

字符串的常用方法

1.charAt(index)

概述: — 从一个字符串中返回指定的字符

var str= "hello world";
console.log(str.charAt(0));  //输出 h
console.log(str.charAt(1));  //输出 e
console.log(str.charAt(999));  //输出 ''

2.charCodeAt(index)

概述: — 获取指定位置处字符的ASCII码

var str = 'hello world';
console.log(str.charCodeAt(0)); //输出 104(h对应的ASCII值)
console.log(str.charCodeAt(4)); //输出 111(o对应的ASCII值)
console.log(str.charCodeAt(99)); //输出NAN

3.concat(string2,string2…)

概述: — 字符串拼接

console.log('hello'.concat('world','!'));  //输出 helloworld!

4.slice(beginIndex[, endIndex])

概述: — 从start位置开始,截取到end位置,end取不到

var str = 'hello world';
console.log(str.slice(4));   //输出  o world
console.log(str.slice(2,6));   //输出  llo          llo后有空格

5.substring(indexStart[, indexEnd])

概述: — 从start位置开始,截取到end位置, end取不到

var str= "Mozilla";
console.log(str.substring(2));    // "zilla"
console.log(str.substring(0,3));  // 输出 "Moz"
console.log(str.substring(3,0));  // "Moz"

6.substr(start[, length])

概述: — 从start位置开始,截取length个字符

var str = "abcdefghij";
console.log( str.substr(1,2));   // (1,2): bc
console.log( str.substr(-3,2));  // (-3,2): hi

7.indexOf(searchValue, fromIndex)

概述: — 返回指定内容在元字符串中的位置 —

var str = "abcdefghij";
console.log( str.indexOf('a'));   // 0
console.log( str.indexOf('cd'));   // 2,返回的是第一个元素(c)的 索引值

8.lastIndexOf(searchValue[, fromIndex])

概述: — 从后往前找,只找第一个匹配的,与indexOf()相反

var str= "Hello new world";
console.log( str.indexOf("w"));  //  indexOf()正向数第一个  "w" 索引=  8 
console.log(str.lastIndexOf("w")); // lastIndexOf 倒向数第一个 "w" 索引=  10

9.trim()

概述: — 去除字符串两边空格

var str1 = '  aabb  ';
console.log(str1.trim());  //输出 'aabb'  
var str2 = '   asd ad    ';  
console.log(str2.trim());  //输出 'asd ad'   ,注意:字符串内部空格不会去除 

10.to(Locale)UpperCase() 和 to(Locale)LowerCase()

概述: — to(Locale)UpperCase() :转换大写 , to(Locale)LowerCase() :转换小写

var str = 'AAbBcC';
console.log(str.toLowerCase());  //aabbcc
console.log(str.toLocaleLowerCase()); //aabbcc
console.log(str.toUpperCase()); //AABBCC
console.log(str.toLocaleUpperCase()); //AABBCC

11.replace(regexp|substr, newSubStr|function)

概述: — 替换原字符,可以使用正则表达式

var str = 'hello world';
var str1 = str.replace('world','java');
 console.log(str1);  //hello java

 var regex = /hello/gi;
 var str2 = str.replace(regex, 'nihao');
 console.log(str2);  //nihao world

 console.log(str);  //hello world  原字符串没有变化

12.split([separator[, limit]])

概述: — 用分隔符将一个String对象分割成子字符串数组,以分割字串来决定每个拆分的位置。

var str = 'ab cd ef ghijk';
var word = str.split(' ');
console.log(word[2]);  //输出 ef

var word2 = str.split('');
console.log(word2[4]);  //输出 d

var word3 = str.split();
console.log(word3);     //输出  ab cd ef ghijk

---------------------------------------------------------------------------

var myString = 'Hello World. How are you?';
var splits = myString.split(' ', 3);
console.log(splits); //输出   ["Hello", "World.", "How"]

在线帮助文档 MDN Web文档 — 前端学习必备

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值