1

In Java I need to construct a string of n zeros with n unknown at compile time. Ideally I'd use

String s = new String('0', n);

But no such constructor exists. CharSequence doesn't seem to have a suitable constructor either. So I'm tempted to build my own loop using StringBuilder.

Before I do this and risk getting defenestrated by my boss, could anyone advise: is there a standard way of doing this in Java? In C++, one of the std::string constructors allows this.

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • Strings are *immutab;e* in java. So, you will have to create a new String once you append "n variable length String". StringBuilder would be the way to go. – TheLostMind Oct 22 '14 at 09:22
  • There is no function in JRE. You need to make your own – talex Oct 22 '14 at 09:22
  • 1
    See this [answer](http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java#4903603) – Reimeus Oct 22 '14 at 09:24

5 Answers5

5

If you don't mind creating an extra string:

String zeros = new String(new char[n]).replace((char) 0, '0');

Or more explicit (and probably more efficient):

char[] c = new char[n];
Arrays.fill(c, '0');
String zeros = new String(c);

Performance wise, the Arrays.fill option seems to perform better in most situations, but especially for large strings. Using a StringBuilder is quite slow for large strings but efficient for small ones. Using replace is a nice one liner and performs ok for larger strings, but not as well as filll.

Micro benchmark for different values of n:

Benchmark                       (n)  Mode  Samples        Score         Error  Units
c.a.p.SO26504151.builder          1  avgt        3       29.452 ±       1.849  ns/op
c.a.p.SO26504151.builder         10  avgt        3       51.641 ±      12.426  ns/op
c.a.p.SO26504151.builder       1000  avgt        3     2681.956 ±     336.353  ns/op
c.a.p.SO26504151.builder    1000000  avgt        3  3522995.218 ±  422579.979  ns/op
c.a.p.SO26504151.fill             1  avgt        3       30.255 ±       0.297  ns/op
c.a.p.SO26504151.fill            10  avgt        3       32.638 ±       7.553  ns/op
c.a.p.SO26504151.fill          1000  avgt        3      592.459 ±      91.413  ns/op
c.a.p.SO26504151.fill       1000000  avgt        3   706187.003 ±  152774.601  ns/op
c.a.p.SO26504151.replace          1  avgt        3       44.366 ±       5.153  ns/op
c.a.p.SO26504151.replace         10  avgt        3       51.778 ±       2.959  ns/op
c.a.p.SO26504151.replace       1000  avgt        3     1385.383 ±     289.319  ns/op
c.a.p.SO26504151.replace    1000000  avgt        3  1486335.886 ± 1807239.775  ns/op
assylias
  • 321,522
  • 82
  • 660
  • 783
0

Create a n sized char array and convert it to String:

char[] myZeroCharArray = new char[n];

for(int i = 0; i < n; i++) myZeroCharArray[i] = '0';

String myZeroString = new String(myZeroCharArray);
Narmer
  • 1,414
  • 7
  • 16
0

See StringUtils in Apache Commons Lang

https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#repeat%28java.lang.String,%20int%29

cadrian
  • 7,332
  • 2
  • 33
  • 42
0

There isn't a standard JDK way, but Apache commons (almost a defacto standard), has the StringUtils.repeat() method, e.g.:

String s = StringUtils.repeat('x', 5); // s = "xxxxx"
BarrySW19
  • 3,759
  • 12
  • 26
0

or the plain old String Format

int n = 10;
String s = String.format("%" + n + "s", "").replace(' ', '0');
System.out.println(s);
TJ-
  • 14,085
  • 12
  • 59
  • 90