君陌离的博客

vuePress-theme-reco 君陌离    2018 - 2020
君陌离的博客 君陌离的博客

Choose mode

  • dark
  • auto
  • light
首页
我的作品
  • 项目橱窗
  • blog模板
分类
  • 数据库
  • CSS
  • 摘记
  • JS
  • Node
  • Vue
  • React
  • GIT
  • Promise
  • Liunx
  • Xshell
  • ajax
  • WINDOWS
  • Python
  • 随笔
  • 脚手架
  • node
  • 自动化
标签
笔记
时间线
About Me
  • 关于我
  • 赞赏
Contact
  • GitHub
  • QQ
author-avatar

君陌离

70

文章

90

标签

首页
我的作品
  • 项目橱窗
  • blog模板
分类
  • 数据库
  • CSS
  • 摘记
  • JS
  • Node
  • Vue
  • React
  • GIT
  • Promise
  • Liunx
  • Xshell
  • ajax
  • WINDOWS
  • Python
  • 随笔
  • 脚手架
  • node
  • 自动化
标签
笔记
时间线
About Me
  • 关于我
  • 赞赏
Contact
  • GitHub
  • QQ
  • HTML5

  • JS

    • JS 前20个常用字符串方法及使用方式
    • JS封装
    • for in,for of, for,forEach,map的区别
    • windowfn方法 npm包引用
    • js中数组操作方法
    • 数组排序的方法
    • 常见的前端跨域解决方案
    • Cookie、session和localStorage、以及sessionStorage之间的区别
    • 数组去重的方法
    • windowfn 常用函数封装(持续更新中)
  • 微信小程序

  • 数据库

  • React

  • Vue

  • vuepress

JS 前20个常用字符串方法及使用方式

vuePress-theme-reco 君陌离    2018 - 2020

JS 前20个常用字符串方法及使用方式

君陌离 2020-05-12 JSString

# 常用的JS字符串函数,列表如下

charAt(x)
charCodeAt(x)
concat(v1,v2..)
fromCharcode(c1,c2)
indexOf(substr, [start])
lastIndexOf(substr, [start])
match(regexp)
replace(regexp/substr, replacetext)
search(regexp)
slice(start, [end])
split(delimiter, [limit])
substr(start, [length])
substring(from, [to])
toLowerCase()
toUpperCase()
includes()
endsWith()
repeat()
valueOf()
trim()

# 1. charAt(x)

  • charAt(x)返回字符串中x位置的字符,下标从 0 开始。
//charAt(x)
var myString = 'jQuery FTW!!!';
console.log(myString.charAt(7));
//output: F
1
2
3
4

# 2.charCodeAt(x)

  • charCodeAt(x)返回字符串中x位置处字符的unicode值。
//charCodeAt(position)
var message="jquery4u"
console.log(message.charCodeAt(1))
//output: 113
1
2
3
4

# 3.concat(v1,v2..)

  • concat() 方法用于连接两个或多个字符串,此方法不改变现有的字符串,返回拼接后的新的字符串。
//concat(v1, v2,..)
var message="Sam"
var final=message.concat(" is a"," hopeless romantic.")
//alerts "Sam is a hopeless romantic."
alert(final)
1
2
3
4
5

# 4.fromCharcode(c1,c2)

  • fromCharcode(c1,c2)转换一组Unicode值转换为字符。
//fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97,98,99,120,121,122))
//output: abcxyz
console.log(String.fromCharCode(72,69,76,76,79))
//output: HELLO
1
2
3
4
5

# 5.indexOf(substr, [start])

  • indexOf方法搜索并(如果找到)返回字符串中搜索到的字符或子字符串的索引。如果没有找到,则返回-1。Start是一个可选参数,指定字符串中开始搜索的位置,默认值为0。
//indexOf(char/substring)
var sentence="Hi, my name is Sam!"
if (sentence.indexOf("Sam")!=-1)
alert("Sam is in there!")
1
2
3
4

# 6.lastIndexOf(substr, [start])

  • lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引, 如果未找到,则返回-1。“Start”是一个可选参数,指定字符串中开始搜索的位置, 默认值为string.length-1。
//lastIndexOf(substr, [start])
var myString = 'javascript rox';
console.log(myString.lastIndexOf('r'));
//output: 11
1
2
3
4

# 7.match(regexp)

  • 根据正则表达式在字符串中搜索匹配项。如果没有找到匹配项,则返回一个信息数组或null。
//match(regexp) //select integers only
var intRegex = /[0-9 -()+]+$/;  
 
var myNumber = '999';
var myInt = myNumber.match(intRegex);
console.log(isInt);
//output: 999
 
var myString = '999 JS Coders';
var myInt = myString.match(intRegex);
console.log(isInt);
//output: null
1
2
3
4
5
6
7
8
9
10
11
12

# 8.replace(regexp/substr, replacetext)

  • replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
//replace(substr, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(/JavaScript/i, "jQuery"));
//output: 999 jQuery Coders
 
//replace(regexp, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(new RegExp( "999", "gi" ), "The"));
//output: The JavaScript Coders
1
2
3
4
5
6
7
8
9

# 9.search(regexp)

  • search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,如果找到,返回与 regexp 相匹配的子串的起始位置,否则返回 -1。
//search(regexp)
var intRegex = /[0-9 -()+]+$/;  
 
var myNumber = '999';
var isInt = myNumber.search(intRegex);
console.log(isInt);
//output: 0
1
2
3
4
5
6
7

# 10.slice(start, [end])

  • slice() 方法可提取字符串的某个部分,返回一个新的字符串。包括字符串从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符。
//slice(start, end)
var text="excellent"
text.slice(0,4) //returns "exce"
text.slice(2,4) //returns "ce"
1
2
3
4

# 11.split(delimiter, [limit])

  • split() 方法用于把一个字符串分割成字符串数组,返回一个字符串数组返回的数组中的字串不包括 delimiter自身。可选的“limit”是一个整数,允许各位指定要返回的最大数组的元素个数。

# 12.substr(start, [length])

  • substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。返回一个新的字符串,包含从 start(包括 start 所指的字符) 处开始的 length 个字符。如果没有指定 length,那么返回的字符串包含从 start 到该字符串的结尾的字符。
//substring(from, to)
var text="excellent"
text.substring(0,4) //returns "exce"
text.substring(2,4) //returns "ce"
1
2
3
4

# 13.substring(from, [to])

  • substring() 方法用于提取字符串中介于两个指定下标之间的字符,返回的子串包括 start 处的字符,但不包括 stop 处的字符,to 可选,如果省略该参数,那么返回的子串会一直到字符串的结尾。
//substring(from, [to])
var myString = 'javascript rox';
myString = myString.substring(0,10);
console.log(myString)
//output: javascript
1
2
3
4
5

# 14.toLowerCase()

  • toLowerCase() 方法用于把字符串转换为小写。
//toLowerCase()
var myString = 'JAVASCRIPT ROX';
myString = myString.toLowerCase();
console.log(myString)
//output: javascript rox
1
2
3
4
5

# 15.toUpperCase()

  • toUpperCase() 方法用于把字符串转换为大写。
//toUpperCase()
var myString = 'javascript rox';
myString = myString.toUpperCase();
console.log(myString)
//output: JAVASCRIPT ROX
1
2
3
4
5

# 16. includes()

  • includes() 方法用于检查字符串是否包含指定的字符串或字符。
//includes()
var mystring = "Hello, welcome to edureka";
var n = mystring.includes("edureka");
//output: True
1
2
3
4

# 17. endsWith()

  • endsWith()函数检查字符串是否以指定的字符串或字符结束。
//endsWith()
var mystr = "List of javascript functions";
var n = mystr.endsWith("functions");
//output: True
1
2
3
4

# 18. repeat()

  • repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。
//repeat()
var string = "Welcome to Edureka";
string.repeat(2);
//output: Welcome to Edureka Welcome to Edureka
1
2
3
4

# 19. valueOf()

  • valueOf() 方法返回一个String对象的原始值(primitive value),该值等同于String.prototype.toString()。
//valueOf()
var mystr = "Hello World!";
var res = mystr.valueOf();
//output: Hello World!
1
2
3
4

# 20. trim()

  • trim() 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LF,CR)

//trim()
var str = "     Hello Edureka!     ";
alert(str.trim());
1
2
3
4

# 阿里面试-字符串操作

<script type="text/javascript">
	var str = "www.taobao.com";
	var res = str.split("").reverse().join("").replace('oat','');
	console.log(res);//moc.oab.www
</script>
1
2
3
4
5
欢迎来到 您的站点名称($site.title)
看板娘