javascript 中的 String类型

本文详细介绍了JavaScript中的String类型及其常用方法,包括字符方法、字符串方法、位置方法等,并提供了丰富的示例代码。

javascript 中的 String类型

String类型是字符串对象的包装类型。可使用String构造函数来创建,不过一般不会用这种方法来创建一个string变量(实例)

var stringObject = new String('hello world');
var stringText = 'hello world';

所有基本的字符串值都继承String对象的方法,如:valueOf()、toLocalString()和toString(),并且都有一个length属性

1.字符方法

1).charAt()
单字符字符串的形式返回给定位置的那个字符

var stringValue = 'hello world';
console.log(stringValue.charAt(1)); // 'e'

2).charCodeAt()
返回给定位置的字符所对应的字符编码

var stringValue = 'hello world';
console.log(stringValue.charCodeAt(1)); // '101'

2.字符串方法

1).concat()
将一或多个字符串拼接起来,返回拼接得到的字符串,可接受任意多个参数

var stringValue = 'hello ';
var res = stringValue.concat('world');
console.log(stringValue); // 'hello '
console.log(res); // 'hello world'

实际开发中还是使用 +(加好操作符)比较多。因为在大多数情况下,比使用 concat()要方便

2).slice(?start, ?end)
start: 指定子字符串的起始位置(可不传,不传返回原字符串
end: 指定字符串到哪个位置结束(可不传,不传默认到原字符串最后一个字符结束
截取字符串,返回一个新的子字符串

当slice() 里面的参数传入负值时,会默认加上原数组的长度

var stringValue = 'hello world';
console.log(stringValue.slice()); // 'hello world';
console.log(stringValue.slice(2)); // 'llo world'
console.log(stringValue.slice(2, 6)); // 'llo '
console.log(stringValue.slice(-9)); // 'llo world'
console.log(stringValue.slice(2, -5)); // 'llo '

3).substr(?start, ?length)
start: 指定子字符串的起始位置(可不传,不传则返回原字符串
length: 指定子字符串的长度(可不传,不传默认原字符串最后一个字符结束
截取字符串,返回一个新的子字符串

substr() 里面的第一个参数为负值时,会默认加上原数组的长度,第二个参数为负值时,会默认转为0

var stringValue = 'hello world';
console.log(stringValue.substr()); // 'hello world';
console.log(stringValue.substr(2)); // 'llo world'
console.log(stringValue.substr(2, 6)); // 'llo wo'
console.log(stringValue.substr(-2)); // 'ld'
console.log(stringValue.substr(2, -6)); // ''

4).substring(?start, ?end)
start: 指定子字符串的起始位置(可不传,不传返回原字符串)
end: 指定字符串到哪里结束(可不传,不传默认到原字符串最后一个字符结束
截取字符串,返回一个新的子字符串

substring() 传入负值时,会将所有负值转为0

var stringValue = 'hello world';
console.log(stringValue.substring()); // 'hello world';
console.log(stringValue.substring(2)); // 'llo world'
console.log(stringValue.substring(2, 6)); // 'llo '
console.log(stringValue.substring(-3)); // 'hello world'
console.log(stringValue.substring(2, -6)); // 'he'

3.字符串位置方法

1).indexOf(char, ?start)
char: 需要查找的字符串
start: 从哪个位置开始向后查找,可不传
从字符串的开头向后搜索子字符串,返回子字符串的位置(没找到则返回-1)

2).lastIndexOf(char, ?start)
char: 需要查找的字符串
start: 从哪个位置开始向前查找,可不传
从字符串的末尾向前搜索子字符串,返回子字符串的位置(没找到则返回-1)

var stringValue = 'hello world';
console.log(stringValue.indexOf('o')); // 4
console.log(stringValue.lastIndexOf('o')); // 7
console.log(stringValue.indexOf('o', 6)); // 7
console.log(stringValue.lastIndexOf('o', 6)); // 4

4.trim() 方法

去除原始字符串中的前置及后缀空格,返回一个新的字符串

var stringValue = '  hello world  ';
console.log(stringValue.trim()); // 'hello world'
console.log(stringValue); // '  hello world  '

5.大小写转换

1).toLowerCase() 和 toLocalLowerCase()
两个方法都是将字符串转为小写toLocalLowerCase 是针对特定地区使用的

var stringValue = 'HELLO WORLD';
console.log(stringValue.toLowerCase()); // 'hello world'
console.log(stringValue.toLocaleLowerCase()); // 'hello world'

2).toUpperCase() 和 toLocalUpperCase()
两个方法都是将字符串转为大写toLocalUpperCase 是针对特定地区使用的

var stringValue = 'hello world';
console.log(stringValue.toUpperCase()); // 'HELLO WORLD'
console.log(stringValue.toLocalUpperCase()); // 'HELLO WORLD'

一般来说,在不知道自己的代码将在哪种语言环境中运行的情况下,还是使用针对地区的方法更稳妥一些

6.模式匹配

1).match()

match() 只接受一个参数,要么是一个正则表达式,要么是一个RegExp 对象
返回一个数组

var test = 'cat, bat, sat, fat';
var pattern = /.at/;
var matches = test.match(pattern);
console.log(matches); // 输出匹配到的东西

2).search()

search() 只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象
返回字符串中第一个匹配性的索引,如果没有,则返回-1

search() 始终是从字符串开头向后找

var test = 'cat, bat, sat, fat';
var index = test.search(/at/);
console.log(index);

3).replace()

replace() 接受两个参数,第一个参数可以是一个RegExp对象 或 一个字符串(这个字符串不会被转换成正则表达式)
第二个参数可以是一个字符串 或 一个函数

replace() 主要是简化替换子字符串的操作

var test = 'cat, bat, sat, fat';
console.log(test.replace('at', 'ond')); // 'cond, bat, sat, fat'
console.log(test.replace(/at/g, 'ond')); // 'cond, bond, sond, fond'

4).split()

可以基于指定的分隔符将一个字符串分割成多个字符串。
第一个参数可以是 一个字符串 或 一个RegExp对象
第二个参数用于指定数组的大小,可不传
返回一个数组

var test = 'red, blue, green, yellow';
console.log(test.split(',')); // '['red', blue', 'green', 'yellow']
console.log(test.split(',', 2)); // ['red', blue']
console.log(test.split(/[^\,]+/)); // ['', '', '', '']

记录总结一下自己学到的东西。可能写得不是很好,有错误的东西请大家指正,互相学习!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值