# String

String 全局对象是一个用于字符串或一个字符序列的构造函数。

字符串字面量采取以下形式:

'string text'
"string text"
"中文/汉语"
"español"
"English "
"हिन्दी"
"العربية"
"português"
"বাংলা"
"русский"
"日本語"
"ਪੰਜਾਬੀ"
"한국어"

# 实例属性

# length

返回字符串的 UTF-16 码元长度。

const x = "Mozilla";
const empty = "";

console.log("Mozilla is " + x.length + " code units long");
/* "Mozilla is 7 code units long" */

console.log("The empty string is has a length of " + empty.length);
/* "The empty string is has a length of 0" */

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# 实例方法

# includes(searchString, position?)

如果 searchString 作为此对象转换为 String 的结果的子字符串出现在大于或等于position的一个或多个位置,则返回 true;否则,返回 false。

参数

名称 类型 必填 描述
searchString string 要在 str 中搜索的字符串。不能是正则表达式。
position number 在字符串中开始搜索 searchString 的位置。(默认为 0。)

返回值

类型 描述
boolean 如果在给定的字符串中找到了要搜索的字符串(包括 searchString 为空字符串的情况),则返回 true,否则返回 false。
const str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# endsWith(searchString, endPosition?)

endsWith() 方法用于判断一个字符串是否以指定字符串结尾,如果是则返回 true,否则返回 false。该方法区分大小写。

参数

名称 类型 必填 描述
searchString string 要搜索的作为结尾的字符串,不能是正则表达式。所有非正则表达式的值都会被强制转换为字符串。
endPosition number 可选,预期找到 searchString 的末尾位置(即 searchString 最后一个字符的索引加 1)。默认为 str.length。

返回值

类型 描述
boolean 如果被检索字符串的末尾出现了指定的字符串(包括 searchString 为空字符串的情况),则返回 true;否则返回 false。
const str1 = 'Cats are the best!';
console.log(str1.endsWith('best!'));
// expected output: true
console.log(str1.endsWith('best', 17));
// expected output: true
const str2 = 'Is this a question?';
console.log(str2.endsWith('question'));
// expected output: false

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# repeat(count)

repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。

参数

名称 类型 必填 描述
count number 介于 0 和 +Infinity 之间的整数。表示在新构造的字符串中重复了多少遍原字符串。

返回值

类型 描述
string 包含指定字符串的指定数量副本的新字符串。
"abc".repeat(0)      // ""
"abc".repeat(1)      // "abc"
"abc".repeat(2)      // "abcabc"
"abc".repeat(3.5)    // "abcabcabc" 参数 count 将会被自动转换成整数。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# startsWith(searchString, position?)

startsWith() 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false。这个方法区分大小写。

参数

名称 类型 必填 描述
searchString string 要搜索的子字符串。
position number 在 str 中搜索 searchString 的开始位置,默认值为 0。

返回值

类型 描述
boolean 如果在字符串的开头找到了给定的字符则返回 true;否则返回 false。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# at(index)

方法接受一个整数值,并返回一个新的 String,该字符串由位于指定偏移量处的单个 UTF-16 码元组成

参数

名称 类型 必填 描述
index number 字符指定偏移量处,允许正整数和负整数,负整数从字符串中的最后一个字符开始倒数。

返回值

类型
string | null
const sentence = 'The quick brown fox jumps over the lazy dog.';
let index = 5;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of 5 the character returned is u"
index = -4;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of -4 the character returned is d"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# charAt(pos)

返回一个由给定索引处的单个 UTF-16 码元构成的新字符串。

参数

名称 类型 必填 描述
pos number 要返回的字符的索引,从零开始。

返回值

类型 描述
string 返回一个字符串,该字符串表示指定 index 处的字符(恰好是一个 UTF-16 码元)。如果 index 超出了 0 – str.length - 1 的范围,charAt() 将返回一个空字符串。
const anyString = "Brave new world";

console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");
// The character at index 0 is 'B'
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
// The character at index 1 is 'r'
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
// The character at index 2 is 'a'
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
// The character at index 3 is 'v'
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
// The character at index 4 is 'e'
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
// The character at index 999 is ''

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# charCodeAt(index)

返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元

参数

名称 类型 必填 描述
index number 一个大于等于 0,小于字符串长度的整数。如果不是一个数值,则默认为 0。

返回值

类型 描述
number 指定 index 处字符的 UTF-16 代码单元值的一个数字;如果 index 超出范围,charCodeAt() 返回 NaN。
const sentence = 'The quick brown fox jumps over the lazy dog.';
const index = 4;
console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
// expected output: "The character code 113 is equal to q"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# fromCharCode(...codes : number[]):string

参数

名称 类型 必填 描述
codes number[] -

返回值

类型
string
console.log(String.fromCharCode(189, 43, 190, 61));
// expected output: "½+¾="
console.log(String.fromCharCode(189, 165999, 190, 61));
// expected output: "½衯¾="

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 - 3.9+ -

# concat(...strings)

将字符串参数连接到调用的字符串,并返回一个新的字符串。

参数

名称 类型 必填 描述
strings string[] T要连接到 str 的一个或多个字符串。

返回值

类型 描述
string 一个包含所提供的多个字符串文本组合的新字符串。
let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# indexOf(searchString, position?)

在字符串中搜索指定子字符串,并返回其第一次出现的位置索引。它可以接受一个可选的参数指定搜索的起始位置,如果找到了指定的子字符串,则返回的位置索引大于或等于指定的数字。

参数

名称 类型 必填 描述
searchString string 要搜索的子字符串。
position number 该方法返回指定子字符串在大于或等于 position 位置的第一次出现的索引,默认为 0。如果 position 大于调用字符串的长度,则该方法根本不搜索调用字符串。如果 position 小于零,该方法的行为就像 position 为 0 时一样。

返回值

类型 描述
number 查找的字符串 searchValue 的第一次出现的索引,如果没有找到,则返回 -1。
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# lastIndexOf(searchString, position?)

搜索该字符串并返回指定子字符串最后一次出现的索引。它可以接受一个可选的起始位置参数,并返回指定子字符串在小于或等于指定数字的索引中的最后一次出现的位置。

参数

名称 类型 必填 描述
searchString string 要搜索的子字符串。
position number 该方法返回指定子字符串在小于或等于 position 的位置中的最后一次出现的索引,默认为 +Infinity。如果 position 大于调用字符串的长度,则该方法将搜索整个字符串。如果 position 小于 0,则行为与 0 相同,即该方法只在索引 0 处查找指定的子字符串。

返回值

类型 描述
number 如果找到了 searchString,则返回最后一次出现的索引,否则返回 -1。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# match(regexp : string | RegExp) : RegExpMatchArray | null;

match() 方法检索字符串与正则表达式进行匹配的结果。

参数

名称 类型 必填 描述
regexp string | RegExp 一个正则表达式对象或者任何具有 Symbol.match 方法的对象。

返回值

类型 描述
RegExpMatchArray | null 一个 Array,其内容取决于是否存在全局(g)标志,如果没有匹配,则返回 null。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 x 4.0 x

# replace(searchValue, replaceValue)

返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式。

参数

名称 类型 必填 描述
searchValue string | RegExp RegExp: 一个RegExp 对象或者其字面量。该正则所匹配的内容会被第二个参数的返回值替换掉。string: 一个将被 newSubStr 替换的 字符串。其被视为一整个字符串,而不是一个正则表达式。仅第一个匹配项会被替换。
replaceValue string 用于替换掉第一个参数在原字符串中的匹配部分的字符串。

返回值

类型 描述
string 一个部分或全部匹配由替代模式所取代的新的字符串。
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# replace(searchValue, replacer)

返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值是一个每次匹配都要调用的回调函数。如果pattern是字符串,则仅替换第一个匹配项。

参数

名称 类型 必填 描述
searchValue string | RegExp RegExp: 一个RegExp 对象或者其字面量。该正则所匹配的内容会被第二个参数的返回值替换掉。string: 一个将被 newSubStr 替换的 字符串。其被视为一整个字符串,而不是一个正则表达式。仅第一个匹配项会被替换。
replacer (substring : string, ...args : any[]) => string 一个用来创建新子字符串的函数,该函数的返回值将替换掉第一个参数匹配到的结果。在iOS中replacer的第二个参数是字符串数组而非可变参数。

返回值

类型 描述
string 一个部分或全部匹配由替代模式所取代的新的字符串。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0
// 不包含捕捉组的示例
let a = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
let b = a.replace(RegExp("fox"),function(match: string, offset: number, string: string):string{
    console.log("match",match)
    console.log("offset",offset)
    console.log("string",string)
    return "cat"
})
console.log("b:",b)

// 包含一个捕获组的示例。注意,目前android仅支持最多五个捕获组
let a1 = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
let b1 = a1.replace(RegExp("(fox)"),function(match: string,p1: string, offset: number, string: string):string{
    console.log("match",match)
    console.log("p1",p1)
    console.log("offset",offset)
    console.log("string",string)
    return "cat"
})
console.log("b1",b1)

search() 方法执行正则表达式和 String 对象之间的一个搜索匹配。

参数

名称 类型 必填 描述
regexp string | RegExp 一个正则表达式(regular expression)对象。

返回值

类型 描述
number 如果匹配成功,则 search() 返回正则表达式在字符串中首次匹配项的索引;否则,返回 -1。
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
// any character that is not a word character or whitespace
const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// expected output: 43
console.log(paragraph[paragraph.search(regex)]);
// expected output: "."

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# slice(start?, end?)

slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。

参数

名称 类型 必填 描述
start number 可选。从该索引(以 0 为基数)处开始提取原字符串中的字符。如果值为负数,会被当做 strLength + beginIndex 看待,这里的strLength 是字符串的长度(例如,如果 beginIndex 是 -3 则看作是:strLength - 3)
end number 可选。在该索引(以 0 为基数)处结束提取字符串。如果省略该参数,slice() 会一直提取到字符串末尾。如果该参数为负数,则被看作是 strLength + endIndex,这里的 strLength 就是字符串的长度 (例如,如果 endIndex 是 -3,则是,strLength - 3)。

返回值

类型 描述
string 返回一个从原字符串中提取出来的新字符串
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// expected output: "the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# split(separator, limit?)

split() 方法接受一个模式,通过搜索模式将字符串分割成一个有序的子串列表,将这些子串放入一个数组,并返回该数组。

参数

名称 类型 必填 描述
separator string | RegExp 描述每个分割应该发生在哪里的模式。
limit number 一个非负整数,指定数组中包含的子字符串的数量限制。当提供此参数时,split 方法会在指定 separator 每次出现时分割该字符串,但在已经有 limit 个元素时停止分割。任何剩余的文本都不会包含在数组中。

返回值

类型 描述
string[] 在给定字符串中出现 separator 的每一个点上进行分割而成的字符串数组。
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"
const chars = str.split('');
console.log(chars[8]);
// expected output: "k"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# substring(start, end?)

返回一个字符串在开始索引到结束索引之间的一个子集,或从开始索引直到字符串的末尾的一个子集。

参数

名称 类型 必填 描述
start number 要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。
end number 可选。一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。

返回值

类型 描述
string 包含给定字符串的指定部分的新字符串。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

bug&tips

当前版本android平台 start 与 end 参数声明为Int, 需要将number 参数转换为int

let a = 1
let b = 2
// 临时解决办法
"hello uts".substring(a.toInt(),b.toInt())

这个问题后续会修复

# toLowerCase()

toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。

返回值

类型 描述
string 一个新的字符串,表示转换为小写的调用字符串。
console.log('中文简体 zh-CN || zh-Hans'.toLowerCase());
// 中文简体 zh-cn || zh-hansconsole.log( "ALPHABET".toLowerCase() );
// "alphabet"

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# toUpperCase()

将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。

返回值

类型 描述
string 一个新的字符串,表示转换为大写的调用字符串。
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# trim()

从字符串的两端清除空格,返回一个新的字符串,而不修改原始字符串。此上下文中的空格是指所有的空白字符(空格、tab、不换行空格等)以及所有行终止符字符(如 LF、CR 等)。

返回值

类型 描述
string 一个表示 str 去掉了开头和结尾的空白字符后的新字符串。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# padStart(targetLength, padString?)

用另一个字符串填充当前字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的开头开始的。

参数

名称 类型 必填 描述
targetLength number 当前 str 填充后的长度。如果该值小于或等于 str.length,则会直接返回当前 str。
padString string 可选。用于填充当前 str 的字符串。如果 padString 太长,无法适应 targetLength,则会从末尾被截断。默认值为“ ”字符(U+0020)。

返回值

类型 描述
string 在开头填充 padString 直到达到给定的 targetLength 所形成的 String。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0
const str1 = '5';
console.log(str1.padStart(2, '0'));
// expected output: "05"

# padEnd(targetLength, padString?)

将当前字符串从末尾开始填充给定的字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的末尾开始的。

参数

名称 类型 必填 描述
targetLength number 当前 str 填充后的长度。如果该值小于或等于 str.length,则会直接返回当前 str。
padString string 可选。用于填充当前 str 的字符串。如果 padString 太长,无法适应 targetLength,则会被截断。默认值为“ ”字符(U+0020)。

返回值

类型 描述
string 在开头填充 padString 直到达到给定的 targetLength 所形成的 String。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0
const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));
// expected output: "Breaded Mushrooms........"
const str2 = '200';
console.log(str2.padEnd(5));
// expected output: "200  "

# includes(searchString, position?)

如果 searchString 作为此对象转换为 String 的结果的子字符串出现在大于或等于position的一个或多个位置,则返回 true;否则,返回 false。

参数

名称 类型 必填 描述
searchString string 要在 str 中搜索的字符串。不能是正则表达式。
position number 在字符串中开始搜索 searchString 的位置。(默认为 0。)

返回值

类型 描述
boolean 如果在给定的字符串中找到了要搜索的字符串(包括 searchString 为空字符串的情况),则返回 true,否则返回 false。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# endsWith(searchString, endPosition?)

endsWith() 方法用于判断一个字符串是否以指定字符串结尾,如果是则返回 true,否则返回 false。该方法区分大小写。

参数

名称 类型 必填 描述
searchString string 要搜索的作为结尾的字符串,不能是正则表达式。所有非正则表达式的值都会被强制转换为字符串。
endPosition number 可选,预期找到 searchString 的末尾位置(即 searchString 最后一个字符的索引加 1)。默认为 str.length。

返回值

类型 描述
boolean 如果被检索字符串的末尾出现了指定的字符串(包括 searchString 为空字符串的情况),则返回 true;否则返回 false。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# repeat(count)

repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。

参数

名称 类型 必填 描述
count number 介于 0 和 +Infinity 之间的整数。表示在新构造的字符串中重复了多少遍原字符串。

返回值

类型 描述
string 包含指定字符串的指定数量副本的新字符串。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# startsWith(searchString, position?)

startsWith() 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false。这个方法区分大小写。

参数

名称 类型 必填 描述
searchString string 要搜索的子字符串。
position number 在 str 中搜索 searchString 的开始位置,默认值为 0。

返回值

类型 描述
boolean 如果在字符串的开头找到了给定的字符则返回 true;否则返回 false。

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# at(index)

方法接受一个整数值,并返回一个新的 String,该字符串由位于指定偏移量处的单个 UTF-16 码元组成

参数

名称 类型 必填 描述
index number 字符指定偏移量处,允许正整数和负整数,负整数从字符串中的最后一个字符开始倒数。

返回值

类型
string | null

兼容性

Android uni-app x iOS uni-app x web uni-app x Android uni-app iOS uni-app web uni-app
3.9 4.11 4.0

# 参见

相关 Bug