标签:操作方法 rem star 位置 索引 log 搜索 转换方法 str
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>字符串的length属性</title> </head> <body> <script type="text/javascript"> //1:创建字符串 var str1=new String(‘Hello World‘);//通过new关键字 var str2=‘Hello World‘;//字面量 console.log(str1.length);//长度为11 console.log(str2.length);//长度为11 </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>字符方法</title> </head> <body> <script type="text/javascript"> var str=‘Hello World‘;//创建字符串 //1:测试charAt()方法 console.log(str.charAt(1));//返回e //2:测试charCodeAt()方法 console.log(str.charCodeAt(1));//返回101(ASCII编码) console.log(str[1]);//返回e </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>字符串的操作方法</title> </head> <body> <script type="text/javascript"> //1:测试concat()方法 var str1=‘Hello ‘; var result=str1.concat(‘World‘); console.log(str1); //Hello console.log(result);//Hello World //2:测试slice(startIndex,[lastIndex])方法 //参数:开始下标,结束下标(可选) var stringValue=‘hello world‘; console.log(stringValue.slice(3));//lo world console.log(stringValue.slice(3,7));//lo w //3:测试substr(startIndex,[lastIndex])方法 //参数:开始下标,结束下标(可选) console.log(stringValue.substr(3));//lo world console.log(stringValue.substr(3,7));// lo worl //4:测试substring(startIndex,[lastIndex])方法 //参数:开始下标,结束下标(可选) console.log(stringValue.substring(3));//lo world console.log(stringValue.substring(3,7));//lo w var item=‘hello world‘; console.log(item.slice(-3));//rld console.log(item.substr(-3));//rld console.log(item.substring(-3));//hello world console.log(item.slice(3,-4));//lo w console.log(item.substr(3,-4));//‘‘空字符串 console.log(item.substring(3,-4));//hel </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>字符串位置方法</title> </head> <body> <script type="text/javascript"> var stringValue=‘hello world‘; //1:测试inexOf()方法 console.log(stringValue.indexOf(‘o‘));//4 console.log(stringValue.indexOf(‘o‘,6));//7 //2:测试lastIndexOf()方法 console.log(stringValue.lastIndexOf(‘o‘));//7 console.log(stringValue.lastIndexOf(‘o‘,6));//4 var item=‘Lorem ipsum dolor sit amet, consectetur adipisicing elit‘; var positions=new Array(); var pos=item.indexOf(‘e‘); while(pos>1){ positions.push(pos); pos=item.indexOf(‘e‘,pos+1); } console.log(positions);//3,24,32,35,52; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>trim()方法</title> </head> <body> <script type="text/javascript"> var str=‘ hello world ‘; var trimStr=str.trim(); console.log(str);// hello world console.log(trimStr);//hello world </script> </body> </html>
<!DOCTYPE html> <html> <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 console.log(str.toLocaleLowerCase());//hello world console.log(str.toLocaleUpperCase());//HELLO WORLD </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>字符串的模式匹配方法</title> </head> <body> <script type="text/javascript"> //1:测试match()方法 var text1=‘cat, bat, sat, fat‘; var pattern=/.at/; var matches=text1.match(pattern); console.log(matches.index);//0 console.log(matches[0]);//cat console.log(pattern.lastIndex);//0 //2:测试search()方法 var text2=‘cat bat, sat, fat‘; var pos=text2.search(/at/); console.log(pos);//1 //3:测试replace()方法 var text3=‘cat, bat, sat, fat‘; var result=text3.replace(‘at‘,‘ond‘); console.log(result);//cond,bat,sat,fat result =text3.replace(/at/g,‘ond‘); console.log(result);//cond,bond,sond,fond //4:测试split()方法 var text4=‘red,blue,green,yellow‘; var colors1=text4.split(‘,‘); var colors2=text4.split(‘,‘,2); console.log(colors1);//[‘red‘,‘blue‘,‘green‘,‘yellow‘]; console.log(colors2);//[‘red‘,‘blue‘]; </script> </body> </html>
标签:操作方法 rem star 位置 索引 log 搜索 转换方法 str
原文地址:https://www.cnblogs.com/jjgw/p/11608617.html