String-apis(31-40)

161 阅读2分钟

(31) small()

方法已废弃

(32) split()

split()方法依据指定字符串分割字符串成一个子字符串数组。

语法:

str.split([separator[, limit]]) 

参数:

参数说明
separator作为拆分的字符串。可以是字符串也可以是正则。空字符就将此字符串完整分割。
limit分割后数组的长度,如果大于分割后的字符串数组长度,已数组长度为准

示例:

const str = "hello" 
str.split('',2) // ['h', 'e']
str.split('',12) // ['h', 'e', 'l', 'l', 'o']

(33) strike()

方法已废弃

(34) sub() & sup()

方法已废弃

(35) substr()

方法已废弃

(36) substring()

substring 提取从 indexStart 到 indexEnd(不包括)之间的字符。

语法:

str.substring(indexStart[, indexEnd])

参数:

参数说明
indexStart开始截取的索引
indexEnd(可选)一个0-字符串长度之间的整数

说明:

  • 如果 indexStart 等于 indexEndsubstring 返回一个空字符串。
  • 如果省略 indexEndsubstring 提取字符一直到字符串末尾。
  • 如果任一参数小于 0 或为 NaN,则被当作 0。
  • 如果任一参数大于 stringName.length,则被当作 stringName.length
  • 如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。见下面的例子。

示例:

var anyString = "Mozilla";

// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
console.log(anyString.substring(3,-3));
console.log(anyString.substring(3,NaN));
console.log(anyString.substring(-2,3));
console.log(anyString.substring(NaN,3));

// 输出 "lla"
console.log(anyString.substring(4,7));
console.log(anyString.substring(7,4));

// 输出 ""
console.log(anyString.substring(4,4));

// 输出 "Mozill"
console.log(anyString.substring(0,6));

// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(0,10));

(37) toLocaleLowerCase() & toLocaleUpperCase()

根据本地主机语言环境把字符串转换为大写格式,并返回转换后的字符串。

(38) toLowerCase() & toUpperCase()

转换字符串的大小写,并返回一个新字符串。(如果调用该方法的值不是字符串类型会被强制转换)。

(39) toSource()

toSource()  方法返回一个代表对象的源代码。 **非标准:**  该特性是非标准的,请尽量不要在生产环境中使用它!

(40) toString()

toString() 方法返回指定对象的字符串形式。

  • 对于 String 对象,toString 方法返回该对象的字符串形式,和 String.prototype.valueOf()方法返回值一样。
  • String 对象覆盖了Object 对象的 toString 方法