《javascript基础补充--String》

2017-11-20  本文已影响0人  code追命

常用方法

属性

event

prototype

  1. 切割、拼接

    • slice/substring(beginSlice[, endSlice]) ;//beginSlice、endSlice索引值,在这个范围内截取字符串,负值的话len-(index);substring取值不能为负数;
    • substr(start[, length]);//start,在这个范围内截取length长度的字符串,负值的话len-(start);
    • split(separator[,limit]) ;//根据separator分离字符串返回数据,limit 是限制返回的最大长度length(可选);
    • concat(string2, string3[, ..., stringN]);//返回原字符串加参数的拼接值
    • padStart(targetLength [, padString]);//从前面拼接字符串;targetLength拼接后长度,padString拼接字符串
    • padEnd(targetLength [, padString]);//从后面拼接字符串;targetLength拼接后长度,padString拼接字符串
  2. 修改大小写

    • toLowerCase() ;//返回 字符串值转为小写形式;
    • toUpperCase() ;//返回 字符串值转为小写形式;
    • toLocaleLowerCase() ;//返回 字符串值转为小写形式;转换规则根据任何本地化特定的大小写映射;
    • toLocaleUpperCase() ;//返回 字符串值转为小写形式;转换规则根据任何本地化特定的大小写映射;
  3. 查询位置,字符串片段

    • search(regexp); //如果传入一个非正则表达式对象,则会使用 new RegExp(obj) 隐式地将其转换为正则表达式对象。返回正则表达式在字符串中首次匹配项的索引。否则,返回 -1。
    • startsWith(searchString [, pos]); //从pos开始的开头是否等于searchString,返回值bool;
    • indexOf(searchValue[, fromIndex]); //顺序在 fromIndex进行搜索,返回调用 String 对象中第一次出现的指定值的索引。
    • lastIndexOf(searchValue[, fromIndex]); //倒叙在 fromIndex进行搜索,返回调用 String 对象中第一次出现的指定值的索引。
    • includes(searchValue[, fromIndex]); //在 fromIndex进行搜索,返回true or false。
    • charAt(index);//从长度-1(length-1)范围内,索引值对应的字符串;index默认值为0;如果指定的 index 值超出了该范围,则返回一个空字符串。
    • charCodeAt(index);//从长度-1(length-1)范围内,索引值对应的字符编码(Unicode);index默认值为0;如果索引超出范围,则返回 NaN。
    • endsWith(searchString [, position]);//当前字符串是否是以另外一个给定的子字符串“结尾”的,返回true,false;searchString关键字,position结束位置
    • codePointAt(pos);//返回当前位置的字符编码;查询不到返回undefined
  4. 消除空格

    • trim();//左右全消除
    • trimLeft();//左消除
    • trimRight();//右消除

not be familiar with

基本不支持(prototype)

String.prototype.match(regexp)

匹配器

var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
    str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
    str3 = "The contract was declared null and void.";
    str1.match("number");   // "number" 是字符串。返回["number"]
    str1.match(NaN);        // NaN的类型是number。返回["NaN"]
    str1.match(Infinity);   // Infinity的类型是number。返回["Infinity"]
    str1.match(+Infinity);  // 返回["Infinity"]
    str1.match(-Infinity);  // 返回["-Infinity"]
    str2.match(65);         // 返回["65"]
    str2.match(+65);        // 有正号的number。返回["65"]
    str3.match(null);       // 返回["null"]

String.prototype.normalize([form]);

转码工具

String.prototype.replace(substr|regexp, newSubStr|function);

替换工具

跟正则配合的比较多。比较复杂

1.替换顺序

var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
// Smith, John
console.log(newstr);

String.prototype@@iterator

迭代器

var string = 'A\uD835\uDC68';

var strIter = string[Symbol.iterator]();

console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // "\uD835\uDC68"

//************************************************************

var string = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A';

for (var v of string) {
  console.log(v);
}
//=> "A"
//=> "\uD835\uDC68"
//=> "B"
//=> "\uD835\uDC69"
//=> "C"
//=> "\uD835\uDC6A"

参考

msdn-内置对象-String
msd-内置对象-String

上一篇 下一篇

猜你喜欢

热点阅读