
String Class repeat() Method in Java with Examples
Jan 28, 2021 · repeat() method is used to return String whose value is the concatenation of given String repeated count times. If the string is empty or the count is zero then the empty string is returned. Syntax: string.repeat(count); Parameter: Accepts an integer count which is the number of times we want to repeat the string.
java - Simple way to repeat a string - Stack Overflow
If you only know the length of the output string (and it may be not divisible by the length of the input string), then use this method: static String repeat(String s, int length) { return s.length() >= length ? s.substring(0, length) : repeat(s + s, length); } Usage demo: for (int i = 0; i < 50; i++) System.out.println(repeat("_/‾\\", i));
JAVA : how to repeat string in multiple lines - Stack Overflow
Oct 11, 2014 · What you want to do is use a nested for loop (i.e. a loop inside a loop). The outer loop will print line by line by first printing the string, then starting new line. A new line is added by System.out.println(); , while the string itself is generated by another (inner) loop.
Generating a Java String of N Repeated Characters - Baeldung
Jan 8, 2024 · Java has a repeat function to build copies of a source string: String newString = "a".repeat(N); assertEquals(EXPECTED_STRING, newString); This allows us to repeat single characters, or multi-character strings:
java - How to repeat "if" statement when output is false - Stack Overflow
Sep 25, 2015 · In order to repeat anything you need a loop. A common way of repeating until a condition in the middle of loop's body is satisfied is building an infinite loop, and adding a way to break out of it. Idiomatic way of making an infinite loop in Java is while(true): System.out.print("Pick a number 1-10: "); int number = input.nextInt();
String repeat() – Repeat string N times in Java - HowToDoInJava
Oct 1, 2022 · Learn to repeat a given string N times, to produce a new string which contains all the repetitions, though a simple Java program using String.repeat() api.
How to Repeat a String in Java - Delft Stack
Mar 11, 2025 · In this article, we’ll explore different techniques for repeating a string, including the use of loops, the StringBuilder class, and the String.repeat() method introduced in Java 11. Each method will be explained with clear code examples to help you understand how to implement them effectively.
Java 11 String API repeat () Method Example - JavaProgramTo.com
May 29, 2019 · Java-W3schools Blog. Quick guide to Java String API repeat () Method Example and Internal Implementation. repeat (int n) method repeat s and concatenate the string n times. Syntax: public String repeat (int count)
Java String repeat Method - Online Tutorials Library
Learn how to use the Java String repeat method to create repeated strings efficiently. Discover syntax, examples, and practical applications.
How to repeat String N times in Java - Studytonight
Jan 22, 2021 · To repeat a string N time using Java code. While working with string there can be need of repeating a string, for that purpose, Java provides repeat () method.
- Some results have been removed