開心生活站

位置:首頁 > IT科技 > 

java,repeat

IT科技2.08W

<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*/

標籤:repeat java