String类
- 属性
length
- 描述:返回字符串的长度
- 方法
-
charAt()
- 语法:str.charAt(index)
- 参数:index,默认为0
- 描述:从一个字符串中返回指定的字符。如果指定的 index 值超出了该范围,则返回一个空字符串。
-
at()
-
语法:str.at(index)
-
参数:index。可以为负数,当传递负数时,支持从字符串末端开始的相对索引。
-
描述:返回一个新的String
注意:索引的方式没有找到会返回undefined,charAt()方法没有找到会返回空字符串。
var str = "Hello World" console.log(str[4]) console.log(str.charAt(4)) console.log(str.at(4)) console.log(str[20]) // undefined console.log(str.charAt(20)) // ""(空串) console.log(str.at(20)) // undefined console.log(str.charAt(-1)) // ""(空串) console.log(str.at(-1)) // d
-
-
includes()
- 语法:str.includes(searchStr, position)
- 参数1(searchStr):要在str中搜索的字符串。不能是正则表达式。
- 参数2(position):可选。在字符串中开始搜索 searchString 的位置。默认为 0。
- 描述:执行区分大小写的搜索,以确定是否可以在另一个字符串中找到一个字符串,存在返回true,不存在返回false
-
indexOf()
- 语法:str.indexOf(searchStr[, position])
- 参数1(searchStr):要搜索的子字符串,强制转换为字符串
- 参数2(position):可选。该方法返回指定子字符串在大于或等于
position
位置的第一次出现的索引,默认为0
。 - 描述:查找的字符串
searchStr
的第一次出现的索引,如果没有找到,则返回-1
。
-
slice()
- 语法:str.slice(beginIndex[, endIndex)
- 参数1(beginIndex):从该索引(以 0 为基数)处开始提取原字符串中的字符。如果值为负数,会被当做
strLength + beginIndex
看待,这里的strLength
是字符串的长度(例如,如果beginIndex
是 -3 则看作是:strLength - 3
) - 参数2(endIndex):可选。在该索引(以 0 为基数)处结束提取字符串。如果省略该参数,
slice()
会一直提取到字符串末尾。如果该参数为负数,则被看作是 strLength + endIndex - 描述:提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
-
substring()
- 语法:str.substring(indexStart[, indexEnd])
- 参数1(indexStart):需要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。
- 参数2(indexEnd):可选。一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。
- 描述:返回包含给定字符串的指定部分的新字符串。
-
split()
- 语法:str.split([separator[, limit]])
- 参数1(separator):指定表示每个拆分应发生的点的字符串。可以是一个字符串或正则表达式。
- 参数2(limit):一个整数,限定返回的分割片段数量。
- 描述:返回源字符串以分隔符出现位置分隔而成的一个
Array
-
toLowerCase()
- 语法:str.toLowerCase()
- 描述:将调用该方法的字符串值转为小写形式,以新字符串返回。
-
toUpperCase()
- 语法:str.toUpperCase()
- 描述:返回一个新的字符串,表示转换为大写的调用字符串。
-
toString()
- 语法:toString()
- 描述:字符串对象的
toString()
方法返回一个字符串,表示指定的字符串。
-
stratWith()
- 语法:str.startWith(searchString[, position])
- 参数1(searchString):要搜索的子字符串
- 参数2(position):可选。在
str
中搜索searchString
的开始位置,默认值为 0。 - 描述:判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回
true
或false
。
-
endWith()
- 语法:str.endWith(searchString[, length])
- 参数1(searchString):要搜索的子字符串。
- 参数2(length):作为
str
的长度。默认值为str.length
。 - 描述:用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回
true
或false
。
-
replace()
-
concat()
- 语法:str.concat(str2, [, ...strN])
- 参数(str2 [, ...strN]):需要连接到
str
的字符串。 - 描述:将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
-
trim()
- 语法:str.trim()
- 描述:从字符串的两端清除空格,返回一个新的字符串
-
trimStart()
或trimLeft()
- 描述:删除字符串开头的空白字符
-
trimEnd()
或trimRight()
- 描述:删除字符串末尾的空白字符
-
padStart()
-
语法:padStart(targetLength[, padString])
- 参数(targetLength):当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。
- 参数(padString):填充字符串。
-
描述:用另一个字符串填充当前字符串(如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。
-
返回值:在原字符串开头填充指定的填充字符串直到目标长度所形成的新字符串。
'abc'.padStart(10); // " abc" 'abc'.padStart(10, "foo"); // "foofoofabc" 'abc'.padStart(6,"123465"); // "123abc" 'abc'.padStart(8, "0"); // "00000abc" 'abc'.padStart(1); // "abc" // 将数字转换为固定长度的字符串 // printf "%0*d" width num function leftFillNum(num, targetLength) { return num.toString().padStart(targetLength, 0); } const num = 123; console.log(leftFillNum(num, 5)); // 预期输出:"00123"
-
- 字符串遍历
-
普通for循环遍历
-
for...of...遍历
var str = "Hello World" // for...of...遍历 for (var char of str) { console.log(char) }