Mikhail Kholodkov
Mikhail Kholodkov

Reputation: 25136

returning a String with X repeated characters without a loop

I stuck a little. I have a method, which should return a new String contains X times repeated character "y". X and Y are arguments of method. So, simple solution wiil be like this:

public String someMethod(int x,char y){
    String result="";
    for (int i=0;i<x;i++) result+=y;
    return result;    
}

And I've tried to figure out, is there any way to do the same just in one line, without looping. For example:

public String someMethod(int x,char y){
    return new StringBuilder().append('y', x);    
}

But there isn't such method for StringBuilder or StringBuffer or etc. Can you give me any suggestions? Thanks in advance.

Updated: So, the solution will be:

public String someMethod(int x,char y){
    return new String(new char[x]).replace("\0", String.valueOf(y))  
}

Thanks for help!

Upvotes: 1

Views: 1946

Answers (7)

Mikhail Kholodkov
Mikhail Kholodkov

Reputation: 25136

Answering my own question almost 5 years later.

In Java 11 it can be done as following:

public String someMethod(int x, char y) {
    return String.valueOf(y).repeat(x);
}

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122364

For java 8 you could use a stream:

Stream.generate(() -> String.valueOf(y))
      .limit(x)
      .collect(Collectors.joining()).toString()

Upvotes: 0

stuXnet
stuXnet

Reputation: 4349

As others are saying, it's not possible completely without a loop - you could hide it behind other methods, but they still use a loop.

If it's just for the sake of "more compact" code, you could write your own method to do exactly that - or you use third party libs like Apache Commons StringUtils : StringUtils.repeat(String str, int repeat) should do the job

Javadoc for StringUtils.repeat(...)

Upvotes: 1

Betlista
Betlista

Reputation: 10549

public String someMethod(int x,char y) {
    StringBuilder buff = new StringBuilder();
    return someMethod( x, y, buff );
}

public String someMethod(int x,char y, StringBuilder buff) {
    if (x == 0) return buff.toString();
    else {
        buff.append( y );
        return someMethod( x, y, buff );
    }
}

...but recursion is not better than loop if this is not a puzzle...

On the other hand, recursion is more general, when someone wants to remove loop comparing to some String realted functions (in this case new String(char[]) and replace())

Upvotes: 0

Tot&#242;
Tot&#242;

Reputation: 1854

You can do

import java.util.Arrays;

public String someMethod(int x,char y){
    char[] a = new char[x];
    Arrays.fill(a, 0, x, y);
    return new String(a);
}

Upvotes: 1

merlin2011
merlin2011

Reputation: 75555

Try this. Here we are generating a new char[] which default initializes to all 0's, and then replacing these 0's with the character we want.

public class B {

    public static void main(String[] args) {            
        int x = 5;
        String y = "h";
        String result = new String(new char[x]).replace("\0", y);
        System.out.println(result);
    }
}

Upvotes: 1

Eric
Eric

Reputation: 24890

Use StringBuffer & loop, without loop this can't be done.
Even you found a method that could do it without loop, inside it's still use loop.

Upvotes: 0

Related Questions