What is the easiest way to repeat a string in Java?

Asked 2 years ago, Updated 2 years ago, 42 views

I'm looking for a method or operator that allows me to repeat the string n times. I know how to spin a loop I want to use a simpler and more direct method than turning an unnecessary loop.

String str = "abc";
String repeated = str.repeat(3);

repeated.equals("abcabcabc");

Like this.

The reason I avoid repeating myself is because

string java

2022-09-21 16:21

1 Answers

above java 1.5 repeated = new String(new char[n]).replace("\0", s); You can do it like this. n is the variable for how many times you want to repeat the string s.

You don't need to import anything.


2022-09-21 16:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.