String-apis(11-20)

159 阅读2分钟

(11) fixed()

MDN提示此方法已废弃

(12) fontcolor()

MDN提示此方法已废弃

(13) fontsize()

MDN提示此方法已废弃

(14) fromCharCode()

静态 String.fromCharCode()  方法返回由指定的 UTF-16 代码单元序列创建的字符串。

参数: 一系列UTF-16 代码单元的数字。范围介于 0 到 655350xFFFF)之间。大于 0xFFFF 的数字将被截断。不进行有效性检查。

示例:

String.fromCharCode(65, 66, 67);   // 返回 "ABC"
String.fromCharCode(0x2014);       // 返回 "—"
String.fromCharCode(0x12014);      // 也是返回 "—"; 数字 1 被剔除并忽略
String.fromCharCode(8212);         // 也是返回 "—"; 8212 是 0x2014 的十进制表示

(15) fromCodePoint()

String.fromCodePoint() 静态方法返回使用指定的代码点序列创建的字符串。

说明:

  • 该方法返回一个字符串,而不是一个 String 对象。
  • 如果传入无效的 Unicode 编码

示例:

String.fromCodePoint(42);       // "*"
String.fromCodePoint(65, 90);   // "AZ"
String.fromCodePoint(0x404);    // "\u0404"
String.fromCodePoint(0x2F804);  // "\uD87E\uDC04"
String.fromCodePoint(194564);   // "\uD87E\uDC04"
String.fromCodePoint(0x1D306, 0x61, 0x1D307) // "\uD834\uDF06a\uD834\uDF07"

String.fromCodePoint('_');      // RangeError
String.fromCodePoint(Infinity); // RangeError
String.fromCodePoint(-1);       // RangeError
String.fromCodePoint(3.14);     // RangeError
String.fromCodePoint(3e-2);     // RangeError
String.fromCodePoint(NaN);      // RangeError

(16) includes()

includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。

说明:

  • 区分大小写

参数:

参数描述
searchString要搜索的字符串
position从当前字符串的哪个索引位置开始搜索子字符串

示例:

"hello world".includes("wo") // true
"hello world".includes("el",4) // false

(17) indexOf() & lastIndexOf()

1.indexOf()  方法返回调用它的 [String]对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。

参数:

参数描述
searchString要搜索的字符串
position从当前字符串的哪个索引位置开始搜索子字符串

示例:

'hello world'.indexOf("ll") // 2
'hello world'.indexOf("ll",4) // -1

2.lastIndexOf()方法返回调用[String] 对象的指定值最后一次出现的索引,在一个字符串中的指定位置 fromIndex处从后向前搜索。如果没找到这个特定值则返回-1 。

(18) italics()

MDN提示此方法已废弃

(19) link()

MDN提示此方法已废弃

(20) localeCompare()

localeCompare() 方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同。