js之String()函数

String()为构造函数,用于字符的类型的转换和字符串操作。
1.所有东西都能往里塞,并转换成字符类型。

var str = String(123);
console.log(str);//123
var str = String('hello world');//hello world
console.log(str);
var str = String(true);
console.log(str);
var str = String(null);
console.log(str);//null
var str = String(undefined);
console.log(str);//undefined
var str = String({});
console.log(str);// [object Object]
var str = String([123]);
console.log(str);//123

2.String()让字符具有Array的特性

var str = new String('hello world');
console.log(str);

返回:
在这里插入图片描述

for(var i = 0; i < str.length; i++){
    console.log(str[i]);
}

返回
在这里插入图片描述
}
2.String()的函数
2.1 indexOf()/lastindexOf()
var str = new String(‘hello world’);
console.log(str.indexOf(‘r’));//8,lastIndexOf()从右边数起,返回右边第一个位置,如果找不到返回-1;
2.2 charAt()输入字符串位置,返回字符串

var str = new String('hello world');
console.log(str.charAt(6));//w

2.3 substring(x,y);截取一段字符,x为起始位置,y为终止位置,但不包括y,该函数命名不遵守小驼峰命名规则

var str = new String('hello world');
console.log(str.substring(0,5));//hello

2.4 replace(‘x’,‘y’),用’y’代替’x’;

var str = new String('hello world');
console.log(str.replace('l','Y'));//heYlo world;只替代左边数起第一个字符,
//若想全部替换需要正则表达式(严格规格表达式)
console.log(str.replace(/l/g,'Y'));//heYYo worYd——g为golbal全部的意思。

2.5 split()将字符串转化为数组

var str = new String('hello world');
console.log(str.split(''));//["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
String()是JavaScript中的构造函数,用于字符类型的转换和字符串操作。它可以将任何类型的数据转换为字符串类型。例如,使用String(123)将数字123转换为字符串"123",使用String('hello world')将字符串转换为字符串"hello world",使用String(true)将布尔值转换为字符串"true"。同样,使用String(null)将null转换为字符串"null",使用String(undefined)将undefined转换为字符串"undefined",使用String({})将对象转换为字符串"\[object Object\]",使用String(\[123\])将数组转换为字符串"123"。\[1\] 除了基本的转换功能,String对象还可以添加自定义方法。可以通过在String原型上添加方法来扩展String对象的功能,也可以直接在String类上添加方法。例如,可以在String原型上添加isNull方法,也可以在String类上直接添加isNull方法。此外,还可以通过普通函数的实现来定义isNull方法。\[2\] 另外,split()是String对象的一个方法,用于将字符串转换为数组。它接受一个参数作为分隔符,并将字符串按照该分隔符进行拆分,返回一个数组。例如,使用split('')将字符串"hello world"转换为数组\["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"\]。\[3\] #### 引用[.reference_title] - *1* *3* [jsString()函数](https://blog.csdn.net/qq_40794478/article/details/88595061)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JavaScriptString对象添加方法](https://blog.csdn.net/qq_34814092/article/details/109754968)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值