-2

How to add to custom position in a string variable number of custom characters not using any loops? What is the most efficient way?

if(s.length() < width) {  //add spaces
            n = width - s.length();
            s = (' '*n ) + s; // pseudo code
       }
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185

1 Answers1

1

Try like this:

String repeated = new String(new char[n]).replace("\0", yourChar);
String s = input.substring(0, x) + repeated + input.substring(x+1);

Where n is the amount of times you want the String yourChar repeated (which can also be longer than just one Character)

From: Link.

Community
  • 1
  • 1
Blub
  • 3,762
  • 1
  • 13
  • 24