当前位置: 代码网 > it编程>编程语言>Javascript > 前端js经典之字符串超全方法总结大全

前端js经典之字符串超全方法总结大全

2024年09月07日 Javascript 我要评论
1. anchor 创建 a 标签字符串anchor(name),name:a 标签的 name 属性返回一个 a 标签字符串let str = "yqcoder";// 不传值str.anchor(

1. anchor 创建 a 标签字符串

anchor(name),name:a 标签的 name 属性

返回一个 a 标签字符串

let str = "yqcoder";

// 不传值
str.anchor(); // '<a name="undefined">yqcoder</a>'

// 创建 name 属性为 web 的 a 标签字符串
str.anchor("web"); // '<a name="web">yqcoder</a>'

2. at 根据下标查找字符

at(index?),index:字符串下标,默认等于 0

返回下标所在字符串,如果没找到返回 undefined

let str = "yqcoder";

// 默认返回第 1 个字符
str.at(); // 'y'

// 查找最后 1 个字符
str.at(-1); // 'r'

// 没找到
str.at(100); // undefined

3. big 创建大号标签字符串

big(),返回大号标签字符串

let str = "yqcoder";

// 创建 str 大号标签字符串
str.big(); // '<big>yqcoder</big>'

4. blink 创建闪动标签字符串

blink(),返回闪动标签字符串

let str = "yqcoder";

// 创建 str 闪动标签字符串
str.big(); // '<blink>yqcoder</blink>'

5. bold 创建粗体标签字符串

bold(),返回粗体标签字符串

let str = "yqcoder";

// 创建 str 粗体标签字符串
str.bold(); // '<b>yqcoder</b>'

6. charat 查找下标对应字符

charat(index?),index:字符下标,默认为 0

查找到返回对应字符,如果没找到返回空 ""。

let str = "yqcoder";

// 默认返回第 1 位字符
str.charat(); // y

// 返回最后 1 位字符
str.charat(str.length - 1); // r

// 找不到
str.charat(100); // ""

7. charcodeat 查找下标对应字符的 unicode 编码。

charcodeat(index?),index:字符下标,默认为 0

查找到返回对应字符,如果没找到返回空 nan。

let str = "yqcoder";

// 默认返回第 1 位字符
str.charcodeat(); // 121

// 返回最后 1 位字符
str.charcodeat(str.length - 1); // 114

// 找不到
str.charcodeat(100); // nan

8. codepointat 查找下标对应字符的 unicode 编码。

codepointat(index?),index:字符下标,默认为 0

查找到返回对应字符,如果没找到返回空 undefined。

let str = "yqcoder";

// 默认返回第 1 位字符
str.codepointat(); // 121

// 返回最后 1 位字符
str.codepointat(str.length - 1); // 114

// 找不到
str.codepointat(100); // undefined

9. concat 字符串拼接

concat(arg),arg 参数可以是字符串,数组,多层数组,多个数组。

返回拼接好的字符串

let str = "yqcoder";

// 拼接普通字符串
str.concat(" is coder"); // 'yqcoder is coder'

// 拼接字符串数组
str.concat([" is coder", " is a man"]); // 'yqcoder is coder, is a man'

// 拼接多层字符串数组
str.concat([" is coder", [" is a man", [" is a big"]]]); // 'yqcoder is coder, is a man, is a big'

// 拼接多个数组字符串
str.concat([" is coder"], [" is a man"]); // 'yqcoder is coder is a man'

10. endswith 判断字符串是否由特定字符结尾

endswith(str, length?),str:特定字符,length:被用来判断的字符长度,从左到右的字符长度。

判断时区分大小写,符合返回 true,不符合返回 false

let str = "yqcoder";

// 判断是否coder结尾
str.endswith("coder"); // true

// 判断是否coder结尾
str.endswith("coder"); // false

// 判断前 6 个字符是否以 coder 结尾
str.endswith("coder", 6); // false

11. fixed 创建<tt>标签字符串

fixed(),返回<tt>标签字符串

let str = "yqcoder";

// 创建 str <tt>标签字符串
str.fixed(); // '<tt>yqcoder</tt>'

12. fontcolor 创建 color 属性的 font 标签字符串

fontcolor(color),color:颜色

返回 color 属性的 font 标签字符串

let str = "yqcoder";

// 创建color为red的<font>标签字符串
str.fontcolor("red"); // '<font color="red">yqcoder</font>'

13. fontsize 创建 size 属性的 font 标签字符串

fontsize(size),size:字体大小

返回 size 属性的 font 标签字符串

let str = "yqcoder";

// 创建size为red的<font>标签字符串
str.fontsize(12); // '<font size="12">yqcoder</font>'

14. includes 判断是否包含特定字符串

includes(str, start?),str:特定字符串,start:开始下标

包含返回 true,不包含返回 false

let str = "yqcoder";

// 判断是否包含 'yq' 字符
str.includes("yq"); // true

// 从下标 1 开始判断是否包含 'yq'
str.includes("yq", 1); // false

16. indexof 查找字符串下标

indexof(str, start?),str:查找字符,start:开始下标,默认从 0 开始

查找到返回下标,如果没找到返回 -1。

let str = "yqcoder-yqcoder";

// 从下标 0 开始,查找 y 所在位置。
str.indexof("y"); // 0

// 从下标 2 开始,查找 y 所在位置。
str.indexof("y", 2); // 8

17. iswellformed 判断字符串是否包含单独代理项

iswellformed(),返回 boolean 值。

let str = "yqcoder";
let str1 = "yqcoder\ud800";

// 判断str是否包含单独代理项
str.iswellformed(); // true

// 判断str1是否包含单独代理项
str1.iswellformed(); // false

18. italics 创建斜体标签字符串

italics(),返回斜体标签字符串

let str = "yqcoder";

// 创建斜体字符串
str.italics(); // '<i>yqcoder</i>'

19. lastindexof 从后往前查找字符串下标

lastindexof(str, start?),str:查找字符,start:开始下标,默认从 0 开始

查找到返回下标,如果没找到返回 -1。

let str = "yqcoder-yqcoder";

// 从最后 1 开始,查找 y 所在位置。
str.indexof("y"); // 8

// 从下标 0 开始,查找 y 所在位置。
str.indexof("y", 0); // 0

20. link 创建 a 标签字符串

link(url),url:a 标签的 href 属性

返回一个 a 标签字符串

let str = "yqcoder";

// 不传值
str.link(); // '<a href="undefined" rel="external nofollow" >yqcoder</a>'

// 创建 href 属性为 www.baidu.com 的 a 标签字符串
str.link("www.baidu.com"); // '<a href="www.baidu.com" rel="external nofollow" >yqcoder</a>'

21. localecompare 比较两个字符串大小

localecompare(str),str:比较字符串

当大于比较字符串时返回 1,等于返回 0,小于返回 -1

let str = "a";
let str1 = "b";

// 比较 str,str1 大小
str.localecompare(str1); // -1

// 自身比较
str.localecompare(str); // 0

// 比较 str1,str 大小
str.localecompare(str); // -1

22. match 根据传入正则匹配字符串

match(reg),reg:传入正则

字符串满足正则要求返回一个数组。没找到返回 null

let str = "yqcoder";

// 匹配正则 /coder/ 的字符串
str.match(/coder/); // ['coder', index: 2, input: 'yqcoder_yqcoder', groups: undefined]

// 匹配正则 /coder/g 的字符串
str.match(/coder/g); // ['coder']

// 不匹配正则
str.match(/\d/g); // null

23. matchall 根据传入正则匹配字符串

matchall(reg),reg:正则表达式,必须带 g

调用方法的字符串必须通过 ... 才可调用,满足条件返回分组迭代器,不满足返回 undefined

let str = "yqcoder-yqcoder";

// 匹配正则 /coder/g 的字符串
console.log(...str.matchall(/coder/g)); // ['coder', index: 2, input: 'yqcoder-yqcoder', groups: undefined] ['coder', index: 10, input: 'yqcoder-yqcoder', groups: undefined]

// 不匹配正则
console.log(...str.matchall(/\d/g)); // undefined

24. padend 尾部填充

padend(length, str?),length:填充长度,str:填充字符,默认为空

返回填充后的字符串,如果 length 小于原字符串长度,返回原字符串

let str = "yqcoder";

// 默认填充字符串长度到 10
str.padend(10); // 'yqcoder   '

// 填充 1 直到字符长度到 10
str.padend(10, 1); // 'yqcoder111'

// 填充 123 知道字符长度到 9
str.padend(9, 123); // 'yqcoder12'

// 填充 1 直到字符长度到 1
str.padend(1, 1); // 'yqcoder'

25. padstart 头部填充

padstart(length, str?),length:填充长度,str:填充字符,默认为空

返回填充后的字符串,如果 length 小于原字符串长度,返回原字符串

let str = "yqcoder";

// 默认填充字符串长度到 10
str.padstart(10); // '   yqcoder'

// 填充 1 直到字符长度到 10
str.padstart(10, 1); // '111yqcoder'

// 填充 123 知道字符长度到 9
str.padstart(9, 123); // '12yqcoder'

// 填充 1 直到字符长度到 1
str.padstart(1, 1); // 'yqcoder'

26. repeat 重复字符串

repeat(number), number:重复次数

返回重复后的字符串

let str = "yqcoder";

// 重复 1 次字符串
str.repeat(1); // 'yqcoder'

// 重复 2 次字符串
str.repeat(2); // 'yqcoderyqcoder'

27. replace 替换字符串

replace(oldstr, newstr),oldstr:替换前字符串,newstr:替换后字符串

返回替换后的字符串

let str = "yqcoder-yqcoder";

// 将 yq 替换成 dyb
str.replace("yq", "dyb"); // 'dybcoder-yqcoder'

// 将所有 yq 替换成 dyb
str.replace(/yq/g, "dyb"); // 'dybcoder-dybcoder'

28. replaceall 替换所有满足条件字符串

replaceall(oldstr, newstr),oldstr:替换前字符串,newstr:替换后字符串

返回替换后的字符串

let str = "yqcoder-yqcoder";

// 将 yq 替换成 dyb
str.replaceall("yq", "dyb"); // 'dybcoder-dybcoder'

29. search 查找字符串是否存在

search(str),str:目标字符串

如果查到返回目标字符串下标,如果没找到返回 -1

let str = "yqcoder";

// 查找 'yq'
str.search("yq"); // 0

// 查找 'dyb'
str.search("dyb"); // -1

30. slice 字符串截取

slice(start, end?),start:开始下标,end:结束下标

左闭右开,如果存在返回截取字段,不存在返回 ""

let str = "yqcoder";

// 截取字符串前 2 位
str.slice(0, 2); // 'yq'

// 截取字符串最后 1 位
str.slice(-1); // 'r'

// 截取字符串第 11 位
str.slice(10, 11); // ''

31. small 创建 small 标签字符串

small(),返回一个 small 标签字符串

let str = "yqcoder";

// 创建 small 标签字符串
str.small(); // '<small>yqcoder</small>'

32. split 字符串分割成数组

split(str?, length?),str:分割符,length:分割后数组长度

返回分割后的数组。

let str = "yqcoder-yqcoder";

// 不传参,字符串直接转数组
str.split(); // ['yqcoder-yqcoder']

// 将字符串以 "-" 分割成数组
str.split("-"); // ['yqcoder', 'yqcoder']

// 将字符串以 "-" 分割成数组,只要数组第一项
str.split("-", 1); // ['yqcoder']

33. startswith 判断字符串是否由特定字符开始

startswith(str),str:特定字符

判断时区分大小写,符合返回 true,不符合返回 false

let str = "yqcoder";

// 判断是否 yq 开始
str.startswith("yq"); // true

// 判断是否 yq 开始
str.startswith("yq"); // false

34. strike 创建删除标签字符串

strike(),返回删除标签字符串

let str = "yqcoder";

// 创建删除标签字符串
str.strike(); // '<strike>yqcoder</strike>'

35. sub 创建下标标签字符串

sub(),返回下标标签字符串

let str = "yqcoder";

// 创建下标标签字符串
str.sub(); // '<sub>yqcoder</sub>

36. substr 字符串裁剪

substr(index, length?),index:开始下标,length:裁剪长度

返回裁剪后的字符串,包括 index 下标的字符。如果不传 length,默认裁剪从 index 到最后

let str = "yqcoder";

// 裁剪下标 2 开始的全部字符
str.substr(2); // 'coder'

// 裁剪前 2 位字符
str.substr(0, 2); // 'yq'

37. substring 字符串裁剪

substring(start, end?),start:开始下标,end:结束下标

返回裁剪后的字符串,包括 start 下标的字符。不包括 end 结束下标字符。如果不传 end,默认裁剪到最后

let str = "yqcoder";

// 裁剪下标 2 开始的全部字符
str.substring(2); // 'coder'

// 裁剪前 2 位字符
str.substring(0, 2); // 'yq'

// 裁剪第 2 位
str.substring(1, 2); // 'q'

38. sup 创建上标标签字符串

sup(),返回上标标签字符串

let str = "yqcoder";

// 创建上标标签字符串
str.sup(); // '<sup>yqcoder</sup>'

39. tolocalelowercase 字母转小写

tolocalelowercase(),将所有大写字母转小写

let str = "yqcoder";

// 字母转小写
str.tolocalelowercase(); // 'yqcoder'

40. tolocaleuppercase 字母转大写

tolocaleuppercase(),将所有小写字母转大写

let str = "yqcoder";

// 字母转大写
str.tolocaleuppercase(); // 'yqcoder'

41. tolowercase 字母转小写

tolowercase(),将所有大写字母转小写

let str = "yqcoder";

// 字母转小写
str.tolowercase(); // 'yqcoder'

42. touppercase 字母转大写

touppercase(),将所有小写字母转大写

let str = "yqcoder";

// 字母转大写
str.touppercase(); // 'yqcoder'

43. tostring 转字符串

tostring(),各个数据类型通过 tostring()方法转字符串

let arr = [1, [2, 3, [4, 5, 6, [7, 8, 9]]]];
let obj = { name: "yqcoder" };
let bl = true;
let number = 123;

// 数组转字符串,会将数组内数据平铺用, 拼接成字符串
arr.tostring(); // '1,2,3,4,5,6,7,8,9'

// 对象转字符串
obj.tostring(); // '[object object]'

// 布尔值转字符串
bl.tostring(); // 'true'

// 数字转字符串
number.tostring(); // '123'

44. towellformed 迭代字符串的码元

towellformed(),将任何单独的代理项替换为 unicode 替换字符 u+fffd �。

let str = "yqcoder";
let str1 = "yqcoder\ud800";

// 将 str 中的单独的代理项替换成 �
str.towellformed(); // 'yqcoder'

// 将 str1 中的单独的代理项替换成 �
str1.towellformed(); // 'yqcoder�'

45. trim 去字符串头和尾的空格

trim(),返回去除头尾空格后的字符串,无法去除中间的空格。

let str = "  yq coder  ";

// 将 str 去除头尾空格
str.trim(); // 'yq coder'

46. trimend、trimright 去字符串尾部的空格

trimend()、trimright() 返回去除尾部空格后的字符串。

let str = "  yq coder  ";

// 将 str 去除头尾空格
str.trimend(); // '  yq coder'
str.trimright(); // '  yq coder'

47. trimstart、trimleft 去字符串头部的空格

trimstart()、trimleft() 返回去除头部空格后的字符串。

let str = "  yq coder  ";

// 将 str 去除头尾空格
str.trimstart(); // 'yq coder  '
str.trimleft(); // 'yq coder  '

总结 

到此这篇关于前端js经典题之字符串超全方法的文章就介绍到这了,更多相关前端js字符串方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com