2

I want to initialize a string as follows:

public int function (  int count )  { 
String s_new = "88888...  'count' number of 8's "    <-- How to do this
//Other code 
}

Currently I'm not sure how to do this, so I've declared an int array ( int[] s_new ) instead and Im using for loops to initialize this int array.

EDIT: I meant that I need to initialize a string containing only 8's ... the number of times the digit 8 occurs is 'count' times.

1
  • Is the 88888... part of the string supposed to represent the value of count? ie the user passes in 5, you put 5 eights there? Commented Jan 18, 2014 at 20:28

5 Answers 5

4

You can use Guava's Strings.repeat() method:

String str = Strings.repeat("8", count);
Sign up to request clarification or add additional context in comments.

Comments

3

In these cases, it is recommended to use a StringBuilder:

StringBuilder sb = new StringBuilder();
String s = "";
int count = 8;

for (int i = 0; i < count; i++) {
    sb.append('8');
}

s = sb.toString();

System.out.println(s);

Output:

88888888

5 Comments

This is a more efficient way to accomplish this than mine (memory/time). +1
Java has a pretty smart compiler, all string + operations are actually converted to StringBuilder.append's so I doubt it, but when dealing with 8 characters output you need to be sure to optimize everything
@Philipp I was unaware of how the Java compiler handled such. But, yeah. Those 8 character outputs could take a mighty long time. ;)
@Philipp + is internally implemented via StringBuilder but the compiler will do that in a loop here. new StringBuilder(s).append('8').toString() each time. You have move the builder out of the loop manually
@Philipp not in loops I believe and this isn't part of the Java spec so cannot be relied upon in non-Oracle compilers.
3

Try:

String s_new = "";
for (int i = 0; i < count; i++) {
    s_new += "8";
}
return s_new;

Now, this is a naive solution. A better solution (as is posted in other answers here) will use a StringBuffer or StringBuilder to accomplish this in a more efficient manner.

Also, further reading on the difference between those two options: Difference between StringBuilder and StringBuffer

2 Comments

It compiles fine but is creating a new string object each iteration
@zapl Clarified that this is a naive solution, and not optimal.
2

You can build strings using the StringBuilder class.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++)
    sb.append('8')
String s_new = sb.toString();

s_new would then have as much 8 as you have count.

Comments

1

Solution on pure Java using arrays:

public String repeat(char ch, int count) {
    char[] chars = new char[count];
    Arrays.fill(chars, ch);
    return new String(chars);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.