首页 编程 正文

js charAt()和 charCodeAt()方法的使用(js 查找字符串)

2023-10-09 09:46 23
admin

提取字符串字符

这是两个提取字符串字符的安全方法:

  • charAt(position)
  • charCodeAt(position)

charAt() 方法

charAt() 方法返回字符串中指定下标(位置)的字符串:

实例

var str = "HELLO WORLD";
str.charAt(0);            // 返回 H

charCodeAt() 方法

charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码:

实例

var str = "Https://www.hao366.net";

str.charCodeAt(0);         // 返回 72