js string的一些操作


1. charAt();如果想獲取字符編碼,則:charCodeAt();

var stringValue ="hello world";
alert(stringValue.charAt(4);//"o"

2. 字符串操作:concat(),可接受任意參數和“+”類似.

3. 字符串操作:slice(), substr(), substring();

  a.slice()和substring()的第二個參數指字符串的位置,而substr()第二個參數是指從第一個參數位置開始的個數.

  b.參數是負值的情況下:

    ⒈slice()方法會將傳入的負值與字符串長度相加.

    ⒉substr()方法將負的第一個參數加上字符串長度,第二個參數轉換成0.

    ⒊substring()方法會把所有負值參數都轉換為0.

var stringValue = "hello world";
alert(stringValue.slice(-3)); //rld, -3+11
alert(stringValue.substring(-3)); //hello world, -3=>0
alert(stringValue.substr(-3)); // rld, -3+11
alert(stringValue.slice(3,-4)); // lo w, (3, -4+11)
alert(stringValue.substring(3,-4)); //"", (3,0)

4, indexOf, lastIndexOf

var stringValue ="hello world";
alert(stringValue.indexOf(“o”,6)); //7, 忽略開始的6個字符個數.
alert(stringValue,lastIndexOf("o",6));//4,忽略結束的字符個數.

5. trim(), IE9+.

6. toLowerCase(), toUpperCase(), toLocalLowerCase(), toLocalUpperCase().

7. match,類似RegExp的exec()方法. var stringValue="bat,cat"; var patter=/.at/; stringvalue.match(patter);

8. Search(). 類似match(), 返回字符串的索引項,沒有返回-1.

9. Replace(), //replace("aa","bb"), 替換所有,replace(/aa/g,bb);

10. encodeURI()和encodeURIComponent().

var uri = "http://www.xx.com/illegal value.html#start";
alert(encodeURI(uri));// http://www.xx.com/illegal%20value.html#start
alert(encodeURIComponent(uri));//http%3A%2F%2F.............

 11. Math.max.apply(Math,[1,2,3,4,5,6]);

  Math.Ceil() 執行向上舍入,math.ceil(25.9)=>26, math.ceil(25.5)=>26, math.ceil(25.1)=>26

  Math.floor() 執行向下舍入. math.floor(25.1)=>25, math.floor(25.5)=>

  Math.round() 執行標准舍入,


免責聲明!

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



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