Loading

字符串原型对象的方法

charAt()

charAt() 方法可返回指定位置的字符

请注意,JavaScript 并没有一种有别于字符串类型的字符数据类型,所以返回的字符是长度为 1 的字符串

var str = "Hello world!";
str.charAt(0); // 'H'
str.charAt(1); // 'e'

charCodeAt()

charCodeAt() 方法可返回指定位置的字符的 Unicode 编码。这个返回值是 0 - 65535 之间的整数

方法 charCodeAt()charAt() 方法执行的操作相似,只不过前者返回的是位于指定位置的字符的编码,而后者返回的是字符子串

var str = "Hello world!";
str.charCodeAt(0); // 72
str.charCodeAt(1); // 101

concat()

concat() 方法用于连接两个或多个字符串

该方法没有改变原有字符串,但是会返回连接两个或多个字符串新字符串

var str1 = "Hello";
var str2 = "World";

str1.concat(" ", str2); // "Hello World"
str2.concat(", ", str1); // "World, Hello"

endsWith()

endsWith() 方法用来判断当前字符串是否是以指定的子字符串结尾的(区分大小写)

如果传入的子字符串在搜索字符串的末尾则返回 true,否则将返回 false

第二个参数代表被检索字符串的长度,默认为 str.length

var str1 = "Cats are the best!";
str1.endsWith("best", 17); // true

var str2 = "Is this a question";
str2.endsWith("?"); // false

fromCharCode()

fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串

该方法是 String 的静态方法,字符串中的每个字符都由单独的 Unicode 数字编码指定。使用语法: String.fromCharCode()

String.fromCharCode(72, 69, 76, 76, 79); // 'HELLO'
String.fromCharCode(65, 66, 67); // 'ABC'

indexOf()

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置

如果没有找到匹配的字符串则返回 -1

第二个参数代表开始检索的位置,默认为 0

var str = "Hello world!";

str.indexOf("Hello"); // 0
str.indexOf("World"); // -1
str.indexOf("world"); // 6

str.indexOf("o"); // 4
str.indexOf("o", 5); // 7

lastIndexOf()

lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,如果指定第二个参数 start,则在一个字符串中的指定位置从后向前搜索

如果没有找到匹配的字符串则返回 -1

var str = "Hello world!";

str.lastIndexOf("Hello"); // 0
str.lastIndexOf("World"); // -1
str.lastIndexOf("world"); // 6

str.lastIndexOf("o"); // 7
str.lastIndexOf("o", 5); // 4

includes()

includes() 方法用于判断字符串是否包含指定的子字符串

如果找到匹配的字符串则返回 true,否则返回 false

第二个参数代表开始检索的位置,默认为 0

var str = "Hello world!";

str.includes("Hello"); // true
str.includes("World"); // false
str.includes("world"); // true

str.includes("o"); // true
str.includes("o", 7); // true
str.includes("o", 8); // false

localeCompare()

localeCompare() 用本地特定的顺序来比较两个字符串

a 小于 b,返回负数,大于 b,返回正数,相等返回 0

var a = "réservé"; // with accents, lowercase
var b = "RESERVE"; // no accents, uppercase

a.localeCompare(b); // -1
a.localeCompare(b, "en", { sensitivity: "base" }); // 0

match()

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配

match() 方法将检索字符串 String Object,以找到一个或多个与 regexp 匹配的文本。这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。

var str = "Hello world!";

str.match("world");
// ['world', index: 6, input: 'Hello world!',groups: undefined, length: 1]

str.match("World"); // null
str.match("worlld"); // null
str.match("world!");
// ['world!', index: 6, input: 'Hello world!',groups: undefined, length: 1]

var str = "1 plus 2 equal 3";
str.match(/\d+/g); // ['1', '2', '3']

matchAll()

matchAll() 方法返回所有匹配结果组成的数组

var regexp = /t(e)(st(\d?))/g;
var str = "test1test2";

var array = [...str.matchAll(regexp)];

array[0];
// ['test1', 'e', 'st1', '1' index: 0, input: 'test1test2',groups: undefined, length: 4]
array[1];
// ['test2', 'e', 'st2', '2' index: 5, input: 'test1test2',groups: undefined, length: 4]

replace()

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串

var str = "Mr Blue has a blue house and a blue car";
str.replace(/blue/g, "red"); // 'Mr Blue has a red house and a red car'
str.replace(/blue/, "red"); // 'Mr Blue has a red house and a blue car'

replaceAll()

replaceAll() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串,该函数会替换所有匹配到的子字符串

var str = "Visit Microsoft! Visit Microsoft!";
str.replaceAll("Microsoft", "Runoob"); // 'Visit Runoob! Visit Runoob!'

var str = "Mr Blue has a blue house and a blue car";
str.replaceAll(/blue/gi, "red"); // 'Mr red has a red house and a red car'

search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串

var str = "Visit Runoob!";
str.search("Runoob"); // 6
str.search("Runoobs"); // -1

str.search(/Runoob/i); // 6
str.search(/Runoobs/i); // -1

slice()

slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分

使用 start(包含) 和 end(不包含) 参数来指定字符串提取的部分。

start 参数字符串中第一个字符位置为 0, 第二个字符位置为 1, 以此类推,如果是负数表示从尾部截取多少个字符串,slice(-2) 表示提取原数组中的倒数第二个元素到最后一个元素(包含最后一个元素)。

end 参数如果为负数,-1 指字符串的最后一个字符的位置,-2 指倒数第二个字符,以此类推。

var str = "Hello world!";
str.slice(1, 5); // ello

var str = "Hello world!";
str.slice(0); // 'Hello world!'

var str = "Hello world!";
str.slice(3); // 'lo world!'

var str = "Hello world!";
str.slice(3, 8); // 'lo wo'

var str = "Hello world!";
str.slice(-1); // '!'
str.slice(-2); // 'd!'

split()

split() 方法用于把一个字符串分割成字符串数组

如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割

split() 方法不改变原始字符串

var str = "How are you doing today?";
str.split(); // ['How are you doing today?']
str.split(""); // ['H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ' ', 'd', 'o', 'i', 'n', 'g', ' ', 't', 'o', 'd', 'a', 'y', '?']
str.split(" "); // ['How', 'are', 'you', 'doing', 'today?']
str.split(" ", 3); // ['How', 'are', 'you']
str.split("o"); // ['H', 'w are y', 'u d', 'ing t', 'day?']

substr()

substr() 方法可在字符串中抽取从 开始 下标开始的指定数目的字符。

substr() 的参数指定的是子串的开始位置和长度,因此它可以替代 substring()slice() 来使用

substr() 方法不改变原始字符串

start 是要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推

var str = "Hello world!";
str.substr(2, 3); // 'llo'
str.substr(2); // 'llo world!'

substring()

substring() 方法用于提取字符串中介于两个指定下标之间的字符

substring() 方法返回的子串包括 开始 处的字符,但不包括 结束 处的字符

var str = "Hello world!";
str.substring(3); // 'lo world!'
str.substring(3, 7); // 'lo w'

substring()

substring() 方法用于提取字符串中介于两个指定下标之间的字符

substring() 方法返回的子串包括 开始 处的字符,但不包括 结束 处的字符

var str = "Hello world!";
str.substring(3); // 'lo world!'
str.substring(3, 7); // 'lo w'

toLocaleLowerCase()

toLocaleUpperCase()

toLowerCase()

toUpperCase()

通常,带不带 local 返回值是相同的,只有几种语言(如土耳其语)具有地方特有的大小写映射

var str = "Hello world!";
str.toLowerCase(); // 'hello world!'
str.toLocaleLowerCase(); // 'hello world!'
str.toUpperCase(); // 'HELLO WORLD!'
str.toLocaleUpperCase(); // 'HELLO WORLD!'

toString()

toString() 方法返回一个表示 String 对象的值。

valueOf()

valueOf() 方法可返回 String 对象的原始值。

valueOf() 方法通常由 JavaScript 在后台自动进行调用,而不是显式地处于代码中。

var str = "Hello world!";
str.toString(); // 'Hello world!'
str.valueOf(); // 'Hello world!'
posted @ 2021-09-13 20:34  Frank-Link  阅读(175)  评论(0编辑  收藏  举报