JavaScript | 字符串repeat()方法 (JavaScript | String repeat() Method)
The String.repeat() method in JavaScript is used to generate a string by repeating the calling string n number of times. n can be any integer from o to any possible number in JavaScript.
JavaScript中的String.repeat()方法用于通过将调用字符串重复n次来生成字符串。 n可以是从o到JavaScript中任何可能数字的任何整数。
Syntax:
句法:
String.repeat(n);
Parameter(s):
参数:
The method accepts one parameter which is the number of times a number is repeated.
该方法接受一个参数,该参数是重复数字的次数。
Return value:
返回值:
The return type of this method is string which is the result of the repeating the strings.
此方法的返回类型是string ,这是重复字符串的结果。
Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.
浏览器支持: Chrome,Internet Explorer,Mozilla,Safari,Opera mini。
Example:
例:
str = "IncludeHelp"
//"IncludeHelp"
str.repeat(3)
//"IncludeHelpIncludeHelpIncludeHelp"
str.repeat(0)
//""
str.repeat(str.length)
//"IncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelp"
str.repeat(-1)
/*
VM424:1 Uncaught RangeError: Invalid count value
at String.repeat (<anonymous>)
at <anonymous>:1:5
*/
翻译自: https://www.includehelp.com/code-snippets/string-repeat-method-with-example-in-javascript.aspx