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() 执行标准舍入,

原文地址:https://www.cnblogs.com/aaronthon/p/10640884.html