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
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.
© 2024 OneMinuteCode. All rights reserved.