javascript 字符串的連接和截取


 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <script>
 9         //concat():連接兩個字符串返回一個新字符串,並且不會被修改
10         var str1 = "hello"; 11         var str2 = "123"; 12         var str3 = str1.concat(str2); 13  console.log(str1); 14  console.log(str2); 15  console.log(str3); 16 
17         var str = "I love my family!"; 18  console.log(str); 19         //slice():根據索引值和索引值截取字符串
20  console.log(str.slice(2)); //從索引截取到最后
21  console.log(str.slice(2,5)); //從索引截取,包左不包右
22  console.log(str.slice(-3)); //倒數幾個
23  console.log(str.slice(5,3)); //空字符串
24 
25         //substr():根據索引值和長度截取字符串
26  console.log(str.substr(2)); //從索引截取到最后
27  console.log(str.substr(2,6)); //從索引截~長度個字符串
28  console.log(str.substr(-3)); //倒數幾個
29 
30         //substring():根據索引和索引值截取字符串
31  console.log(str.substring(2)); //從索引截取到最后
32  console.log(str.substring(2,5)); //從索引截取~長度個字符串
33  console.log(str.substring(-1)); //全部截取
34  console.log(str.substring(5,2)); //只能調換
35     </script>
36 </body>
37 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM