L
Lew
Chris said:A C++ std::string has an overloaded constructor
that allows you to pre allocate it with a specific
character as follows:
std::string s( '0', length );
I looked at Java' String class and StringBuilder
and unfortunately couldn't find an equivalent.
Then you missed
String(char[] value)
Not as direct but just as effective if combined with
<http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#fill(char[], char)>
You can do the same thing with StringBuilder's
StringBuilder append(char[] str)
char[] str = new char[LEN];
// already filled with zeros, so
StringBuilder sb = new StringBuilder( str.length ).append( str );
If Java has no operator overloading, how could I
create my own class to return a value?
E.g: [sic]
String s = MyString( '0', length );
I don't see any operator overloading here.
Is that possible or do I have to build a method
for it and then call that?
You have to build a method.
String s = MyString( '0', length ).get();
Or...
String s = new MyString( '0', length ).get();
You will not be able to use this idiom. You can make a MyString that
implements CharSequence and has a "filler" constructor, then use that in the
constructor
<http://java.sun.com/javase/6/docs/a...er.html#StringBuilder(java.lang.CharSequence)>
and then in turn use the constructor
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(java.lang.StringBuilder)>