<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script type="text/javascript"> var str='abc234fedgoodboy'; var str1="hello"; var str2=" 1234 "; var str3='1,2,3,4'; console.log(str); //從下標截取長度爲5的字符串 console.log(str.substr(4,5)); //截取出4-8下標的字符串 console.log(str.slice(4,9)); console.log(str.substring(4,9)); //查找索引爲8的字符及字符編碼 console.log(str.charAt(8)); console.log(str.charCodeAt(8)); //鏈接字符串 console.log(str.concat(str1)); //去除字符先後的空格 console.log(str2.trim()); //小寫轉換爲大寫 var up=str.toUpperCase(); console.log(up); //大寫轉小寫 var lower=up.toLowerCase(); console.log(lower); //字符4的字符替換爲y---未改變原字符串 str.replace('4','y'); console.log(str); console.log(str.replace('4','y')); //將字符串以逗號分割爲數組 ---未改變原字符串 str3.split(','); console.log(str3); console.log(str3.split(',')); //字符串去重 //var str='abc234fedgoodboy'; //indexOf(value,startIndex)-- 找到值爲value的索引 var newSrt='a'; for(var i=1;i<str.length-1;i++){ for(var j=0;j<newSrt.length;j++){ if(newSrt[j]!==str[i]){ if(j===newSrt.length-1){ newSrt=newSrt.concat(str[i]); } }else{ break; } } } console.log(newSrt); //字符串去重 //var str='abc234fedgoodboy'; //indexOf(value,startIndex)-- 找到值爲value的索引 var newSrt='a'; for(var i=1;i<str.length-1;i++){ for(var j=0;j<newSrt.length;j++){ if(newSrt.indexOf(str[i])===-1){ newSrt=newSrt.concat(str[i]); } } } console.log(newSrt); //找到串中重複的值放到新串中 //var str='abc234fedgoodboy'; var newStr1=''; for(var i=0;i<str.length-1;i++){ if(newStr1.indexOf(str[i])===-1&&str.indexOf(str[i],i+1)>=i+1){ newStr1=newStr1.concat(str[i]); } } console.log(newStr1); </script> </body> </html>