字符方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符方法</title> </head> <body> <script type="text/javascript"> /* charat方法和charcodeat方法都接收一个参数,基于0的字符位置 charat方法是以单字符字符串的形式返回给定位置的那个字符 charcodeat方法获取到的不是字符而是字符编码 */ var str=hello world; console.log(str.charat(1));//e console.log(str.charcodeat(1));//101 //还可以使用方括号加数字索引来访问字符串中特定的字符 console.log(str[1]);//e </script> </body> </html>
字符串操作方法
concat方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>concat方法</title> </head> <body> <script type="text/javascript"> var str=hello ; var res=str.concat(world); console.log(res);//hello world console.log(str);//hello 这说明原来字符串的值没有改变 var res1=str.concat(nihao,!); console.log(res1);//hello nihao! 说明concat方法可以接收任意多个参数 //虽然concat方法是专门用来拼接字符串的,但是实践中我们使用最多的还是加操作符+,因为其简易便行 </script> </body> </html>
slice方法、substring方法、substr方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符串操作方法</title> </head> <body> <script type="text/javascript"> /* slice方法:第一个参数指定子字符串开始位置,第二个参数表示子字符串最后一个字符后面的位置 substring方法:第一个参数指定子字符串开始位置,第二个参数表示子字符串最后一个字符后面的位置 substr方法:第一个参数指定子字符串开始位置,第二个参数表示返回的字符个数 这三个方法都会返回被操作字符串的一个子字符串,都接收一或两个参数 如果没有给这些方法传递第二个参数,则将字符串的长度作为结束位置。这些方法也不会修改字符串本身,只是返回一个基本类型的字符串值 */ var str=hello world; console.log(str.slice(3));//lo world console.log(str.substring(3));//lo world console.log(str.substr(3));//lo world console.log(str.slice(3,7));//lo w 7表示子字符串最后一个字符后面的位置 简单理解就是包含头不包含尾 console.log(str.substring(3,7));//lo w console.log(str.substr(3,7));//lo worl 7表示返回7个字符 console.log(str.slice(3,-4));//lo w -4+11=7表示子字符串最后一个字符后面的位置 简单理解就是包含头不包含尾 console.log(str.substring(3,-4));//hel 会转换为console.log(str.substring(3,0)); //此外由于这个方法会将较小数作为开始位置,较大数作为结束位置,所以相当于调用console.log(str.substring(0,3)); console.log(str.substr(3,-4));//空字符串 console.log(str.substring(3,0)); </script> </body> </html>
字符串位置方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符串位置方法</title> </head> <body> <script type="text/javascript"> /* indexof方法和lastindexof方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1 indexof方法是从字符串的开头向后搜索子字符串,lastindexof方法正好相反 这两个方法都可以接收两个参数:要查找的子字符串和查找的位置 */ var str=hello world; console.log(str.indexof(o));//4 console.log(str.lastindexof(o));//7 console.log(str.indexof(o,6));//7 console.log(str.lastindexof(o,6));//4 </script> </body> </html>
trim方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>trim方法</title> </head> <body> <script type="text/javascript"> /* trim方法用来删除字符串前后的空格 */ var str= hello world ; console.log('('+str.trim()+')');//(hello world) console.log('('+str+')');//( hello world ) </script> </body> </html>
字符串大小写转换方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>大小写转换</title> </head> <body> <script type="text/javascript"> var str=hello world; console.log(str.tolowercase());//hello world console.log(str.touppercase());//hello world </script> </body> </html>
字符串模式匹配方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符串模式匹配</title> </head> <body> <script type="text/javascript"> /* match方法:只接受一个参数,由字符串或regexp对象指定的一个正则表达式 search方法:只接受一个参数,由字符串或regexp对象指定的一个正则表达式 search方法返回字符串中第一个匹配项的索引,如果没有匹配项,返回-1 */ var str=cat,bat,sat,fat; var pattern=/.at/; var matches=str.match(pattern); console.log(matches.index);//0 console.log(matches[0]);//cat console.log(pattern.lastindex);//0 //lastindex表示开始搜索下一个匹配项的字符位置,从0算起 var pos=str.search(/at/); console.log(pos);//1 1表示at字符串在原来字符串中第一次出现的位置 </script> </body> </html>
replace方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>replace方法</title> </head> <body> <script type="text/javascript"> var str=cat,bat,sat,fat; var res=str.replace(at,one);//第一个参数是字符串,所以只会替换第一个子字符串 console.log(res);//cone,bat,sat,fat var res1=str.replace(/at/g,one);//第一个参数是正则表达式,所以会替换所有的子字符串 console.log(res1);//cone,bone,sone,fone </script> </body> </html>
split方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>split方法</title> </head> <body> <script type="text/javascript"> /* split方法是基于指定的字符,将字符串分割成字符串数组 当指定的字符为空字符串时,将会分隔整个字符串 */ var str=red,blue,green,yellow; console.log(str.split(,));//[red, blue, green, yellow] console.log(str.split(,,2));//[red, blue] 第二个参数用来限制数组大小 console.log(str.split(/[^\,]+/));// [, ,, ,, ,, ] //第一项和最后一项为空字符串是因为正则表达式指定的分隔符出现在了子字符串的开头,即red和yellow //[^...] 不在方括号内的任意字符 只要不是逗号都是分隔符 </script> </body> </html>
localecompare方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>localecompare方法</title> </head> <body> <script type="text/javascript"> /* 这个方法用于比较两个字符串 1.如果字符串在字母表中应该排在字符串参数之前,则返回一个负数 1.如果字符串等于字符串参数,则返回0 1.如果字符串在字母表中应该排在字符串参数之后,则返回一个正数 */ var str=yellow; console.log(str.localecompare(brick));//1 console.log(str.localecompare(yellow));//0 console.log(str.localecompare(zoo));//-1 </script> </body> </html>
fromcharcode方法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>fromcharcode方法</title> </head> <body> <script type="text/javascript"> /* fromcharcode方法是接收一或多个字符编码,然后将其转换为字符串 fromcharcode方法是string构造函数的一个静态方法 */ console.log(string.fromcharcode(104,101,108,108,111));//hello </script> </body> </html>
找到匹配字符串所在的各个位置
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符串匹配</title> </head> <body> <script type="text/javascript"> /*找到匹配字符串所在的各个位置*/ var str=asadajhjkadaaasdasdasdasd; var position=[]; var pos=str.indexof(d); while(pos>-1){ position.push(pos); pos=str.indexof(d,pos+1); } console.log(position);//[3, 10, 15, 18, 21, 24] </script> </body> </html>
字符串去重
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符串去重</title> </head> <body> <script type="text/javascript"> //string.split() 执行的操作与 array.join 执行的操作是相反的 //split() 方法用于把一个字符串分割成字符串数组。 //join方法用于将字符串数组连接成一个字符串 //如果把空字符串 () 用作 separator,那么 stringobject 中的每个字符之间都会被分割。 var str=aahhgggsssjjj;//这里字符串没有可以分隔的字符,所以需要使用空字符串作为分隔符 function unique(msg){ var res=[]; var arr=msg.split(); //console.log(arr); for(var i=0;i
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>字符串操作</title> </head> <body> <script type="text/javascript"> /* 1.先实现字符串去重 2.然后对去重后的数组用for循环操作,分别与原始数组中各个值进行比较,如果相等则count++,循环结束将count保存在sum数组中,然后将count重置为0 3.这样一来去重后的数组中的元素在原数组中出现的次数与sum数组中的元素是一一对应的 */ var str=aacccbbeeeddd; var sum=[]; var res=[]; var count=0; var arr=str.split(); for(var i=0;i
<script type="text/javascript"> var str = www.taobao.com; var res = str.split().reverse().join().replace('oat',''); console.log(res);//moc.oab.www </script>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js使用技巧整理
js调试debug方法总结
以上就是js字符串操作大全的详细内容。