Question about Java Strings

This should be an easy one for you Java gurus out there. :) I'm trying to emulate some Visual Basic functionality, but haven't been able to find the Java equivalent after much digging around on this site and through the SDK docs.

In VB, there is a function called String that takes an integer indicating a string length, and the ascii value of a character. It returns back a string that is the length you sent in and all characters in the string are initialized to whatever character you specified.

How do I do this in Java?

Thanks,

LadyLJ

[580 byte] By [ladylj] at [2007-9-26 1:41:38]
# 1

you could use the following method:

public String charString(int count, char c) {

char characters[] = new char[count];

for(int i=0; i<characters.length; i++) characters = c;

return new String(characters);

}

>

tbraun at 2007-6-29 2:33:29 > top of Java-index,Archived Forums,Java Programming...
# 2

public static String string(int len, char c) {

StringBuffer buf = new StringBuffer(len);

for (int i = 0; i < len; i++)

buf.append(c);

return buf.toString();

}

schapel at 2007-6-29 2:33:29 > top of Java-index,Archived Forums,Java Programming...
# 3

sorry, the [ i ] after characters was lost:

public String charString(int count, char c) {

char characters[] = new char[count];

for(int i=0; i<characters.length; i++) characters[ i ] = c;

return new String(characters);

}

>

tbraun at 2007-6-29 2:33:29 > top of Java-index,Archived Forums,Java Programming...
# 4
Thanks for your input, guys. :) I thought I'd have to write my own function, but just wanted to make sure there wasn't some fancy constructor out there before I reinvented the wheel. :)
ladylj at 2007-6-29 2:33:29 > top of Java-index,Archived Forums,Java Programming...
# 5
char [] ara = new char[325];Arrays.fill( ara, '?' );String str = new String( ara );
callMeJay at 2007-6-29 2:33:29 > top of Java-index,Archived Forums,Java Programming...