javascript - 字符串比較


1. sort是排序根據字符的ASCIll碼排序的,不分字符串或其它元素(僅適用於數組)

2. split是將字符串轉為數組形式

3. join是將數組轉為字符串形式

4. indexOf找到元素后返回下標位置,否則返回-1

 1      var answer_a = 'AB';  2      var answer_b = 'BACDSGDGGD';  3 
 4         /*
 5  * 字符串進行比較是否相同.  6  * 答案集:AD  7  * 答案A:AD => 返回下標(已找到)  8  * 答案B:DA => 返回-1(未找到)  9         */
10         function atob(str) { 11             var temp_a = str; 12             var temp_b = temp_a.split(""); 13  temp_b.sort(); 14             var result = temp_b.join(""); 15             return result; 16  } 17 
18  console.log(atob(answer_b).indexOf(answer_a), answer_a, atob(answer_b)); 19         // 0 "AB" "ABCDDDGGGS"

 


免責聲明!

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



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