[JS] 第八课:字符串

1. String 作为构造函数

var str = new String ("hello world");     //str---->object
var str1 = "hello world";     //str1---->string

2.String 作为工具函数

String(5);   //"5"

3.String 构造函数上的方法

  1. String.fromCharCode(…codeUnits)
    功能:使用指定的unicode值创建字符串
    只能表示出16位编码表示的字符
String.fromCharCode(65);   //"A"
String.fromCharCode(0x41);  //"A"
String.fromCharCode(0x2F804); //不能正确识别
  1. String.fromCodePoint(…codeUnits)
    功能:使用指定的unicode值创建字符串
String.fromCodePoint(0x2F804); //你
String.fromCodePoint(0x41);  //"A"
  1. str.length
"ABCD".length;   //4

4. String 方法

  1. String.prototype.charAt(index)
  • 功能:从一个字符串中返回指定位置的字符
  • index:需返回的字符的位置
"ABCD".charAt(1);   //"B"
  1. String.prototype.charCodeAt(index)
  • 功能:从一个字符串中返回指定位置的字符编码
  • 只能正确返回16位的编码
"ABCD".charCodeAt(1);  //66
  1. String.prototype.codePointAt(index)
  • 功能:从一个字符串中返回指定位置的字符编码
"你2".codePointAt(0);   //得到'你'的编码
"你2".codePointAt(2);   //得到'2'的编码,因为‘你’占了四个字节,所以‘2’的索引标签为2 
//无论字符的编码长度,取出字符串中每个字符的编码
let s='你2';
for (let ch of s)
{
	console.log(ch.codePointAt(0));
}
  1. String.prototype.startsWith(searchString[,position])
  • 功能:判断字符串是否以给定的子串为开头
  • 参数:
    searchString—搜索的子字符串
    position—搜索子字符串的开始位置,默认值为0
  • 返回值:布尔值
"ABCDEFG".startsWith("ABC");   //true
  1. String.prototype.endsWith(searchString[,position])
  2. String.prototype.indexOf(searchValue[,fromIndex])
  • 功能:获取指定值在字符串中第一次出现的索引
  • searchValue:要查找的值
  • fromIndex:搜索子字符串的开始位置,默认值为0
  • 如果没有找到返回-1
  1. String.prototype.lastIndexOf(searchValue[,fromIndex])
  • 功能:获取指定值在字符串中最后一次出现的索引
  1. String.prototype.includes(searchString[,position])
  • 功能:判断一个字符串中是否包含搜索的字符串
  • searchValue:要查找的值
  • position:搜索子字符串的开始位置,默认值为0
  • 返回值:布尔值
  1. String.prototype.search(regexp)
  • 功能:从一个字符串中返回与正则表达式匹配的索引值
  • 参数:正则表达式
  • 返回值:首次匹配项的索引值,如果没有则返回-1
"12345abcde".search(/[0-9]/);  //0
  1. String.prototype.match(regexp)
  • 功能:从一个字符串中返回与正则表达式匹配的匹配项
  • 参数:正则表达式
  • 返回值:匹配项数组,如果没有则返回null
"12345abcde".match(/[0-9]/);  //["1"]

在这里插入图片描述

  1. String.prototype.replace(a,b)
  • 功能:根据规则替换字符串中的某些值
  • 参数:
    a:正则表达式/字符串(被替换量)
    b:字符串/创建新字符串的方程(新量)
  • 返回值:替换后的新字符串
  1. String.prototype.substring(indexStart[,indexEnd])
  • 功能:从一个字符串中返回开始索引和结束索引之间的字符串,不包括结束索引位置对应的字符
  • 参数:开始索引,结束索引
    (如果开始索引为负数,则从0开始;结束索引大于字符串长度,则当作字符串长度处理)
  • 返回值:子字符串
  1. String.prototype.slice(beginSlice[,endSlice])
  • 功能:从一个字符串中返回开始索引和结束索引之间的字符串,不包括结束索引位置对应的字符
  • 参数:开始索引,结束索引
    (如果开始/结束索引为负数,则从(字符串长度-abs(开始/结束索引))开始/结束)
  • 返回值:子字符串
  1. String.prototype.substr(start[,length])
  • 功能:从一个字符串中返回从指定位置开始到指定字符数的字符
  • 参数:
    start:开始位置
    (start取负数是与String.prototype.slice处理相同)
    length:提取字符数
  • 返回值:子字符串
  1. String.prototype.split([separator][,limit])
  • 功能:根据分隔符字符串,将字符串分割成字符串数组
  • 参数:
    seporator:分隔符字符串,可以是字符串或正则表达式
    limit:限定返回的分割片段数量,整数,为可选参数
  • 返回值:字符串数组
"hello world".split(" ");   //["hello","world"]
  1. String.prototype.concat(str1,str2[,str3…,strn])
  • 功能:将一个或多个字符串与原字符串进行拼接,形成一个新的字符串
  • 参数:n个与原字符串进行拼接的字符串
  • 返回值:拼接后的新字符串
    (!!相同的效果可以用‘+’完成,如:"12345"+"@qq.com"
"12345".concat("qq.com");    //12345@qq.com
  1. String.prototype.toLowerCase()
  • 功能:将字符串转换成小写形式
  • 参数:无
  • 返回值:转换成小写的字符串
  1. String.prototype.toUpperCase()
  • 功能:将字符串转换成大写形式
  • 参数:无
  • 返回值:转换成大写的字符串
  1. String.prototype.repeat(count)
  • 功能:将字符串重复n遍,生成新的字符串
  • 参数:count:重复的次数
  • 返回值:新的字符串
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值