-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
  • 12,049
  • 39
  • 108
  • 171

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,642
  • 1
  • 11
  • 24