java - repeat

来源:趣味经验馆 2.04W

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

java repeat是什么?让我们一起来了解一下吧!

Javarepeat是通过使用简单的Java程序,学习重复给定字符串N次,以创造含有所有重复的新字符串。使用方法sting.repeat(N)和使用常规方法该表达式可在java10中使用。

 

java repeat
   

String.repeat():返回一个字符串,该字符串的值是给定字符串的重复 count 次的串联。如果字符串为空或 count 为零,则返回空字符串。

/*** Parameters:* count - number of times to repeat** Returns:* A string composed of this string repeated count times or the empty string if this string is empty or count is zero** Throws:* IllegalArgumentException - if the count is negative.*/

实战演练,具体步骤如下:

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 ()    at :1:5*/

热门标签