Formatting a string in Java

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)>
 
C

Chris ( Val )

Chris said:
A C++ std::stringhas an overloaded constructor
that allows you to pre allocate it with a specific
character as follows:
std::strings( '0', length );
I looked atJava'Stringclass and StringBuilder
and unfortunately couldn't find an equivalent.

Then you missed
String(char[] value)

No, I didn't miss it, its just not the same thing.

The C++ std::string has many constructor overloads,
and can even create temporary strings as shown in
my example (notice there is no identifier declared).

Additionally, it is not limited to default padding,
and I can put any fill character in there I like:

std::strings( 'X', length );
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 );

Yes, I knew you could do that, but it's not quite
the same thing, and does not offer the same
advantage in being able to use different characters
for padding.
IfJavahas no operator overloading, how could I
create my own class to return a value?
E.g: [sic]
Strings = MyString( '0', length );

I don't see any operator overloading here.

Technically, I guess it is a conversion operator.

In either case, the objective is to return an
appropriate data type back to the caller via
assignment, where the data returned would in
this contect be the modified string that was
created via its constructor.
You have to build a method.

Thats what I was afraid of :)
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/api/java/lang/StringBuilder.html#St...)>
and then in turn use the constructor
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(ja...)>

Ok, I will take a look.

Thanks again,

Chris
 
L

Lew

Lew said:
No, I didn't miss it, its just not the same thing.

The C++ std::string has many constructor overloads,
and can even create temporary strings as shown in
my example (notice there is no identifier declared).

Additionally, it is not limited to default padding,
and I can put any fill character in there I like:

std::strings( 'X', length );

As I said, and you quoted,

, which allows you to fill with any character you like.
 
C

Chris ( Val )

Lew said:
String(char[] value)
No, I didn't miss it, its just not the same thing.
The C++ std::stringhas many constructor overloads,
and can even create temporary strings as shown in
my example (notice there is no identifier declared).
Additionally, it is not limited to default padding,
and I can put any fill character in there I like:
std::strings( 'X', length );

As I said, and you quoted,

Well yes, but my point was that it requires two operations
instead of one (before I fit it into a String or StringBuilder
object), and it is impossible to work with a temporary in the
same way you can with the C++ std::string.

E.g:

char[] foo = new char[ L1 - L2 ];
Arrays.fill( foo, '0' );

String x = new String( foo ) + arg;

, which allows you to fill with any character you like.

Yes, it does, but in a quirky kind of way, I think :)

Also, I'm not sure what you were thinking here, but I
don't think the following is quite correct:

== said:
char[] str = new char[LEN];
// already filled with zeros, so
StringBuilder sb = new StringBuilder( str.length ).append( str );
== </QUOTE FROM EARLIER POST> ==

It looks like you are implying that by providing 'str.length'
to the StringBuilder' constructor, that it will be initialised
with 'zeros' == to 'str.length' before appending 'str'.

Passing an integer to the StringBuilder constructor only
increases it's capacity (which is 16 by default anyway),
and does not add any elements to it.

Cheers,

Chris
 
L

Lew

Chris said:
Also, I'm not sure what you were thinking here, but I
don't think the following is quite correct:

== said:
char[] str = new char[LEN];
// already filled with zeros, so
StringBuilder sb = new StringBuilder( str.length ).append( str );
== </QUOTE FROM EARLIER POST> ==

It looks like you are implying that by providing 'str.length'
to the StringBuilder' constructor, that it will be initialised
with 'zeros' == to 'str.length' before appending 'str'.

Not at all. No implication at all, much less that one.
Passing an integer to the StringBuilder constructor only
increases it's [sic: should be "its"] capacity (which is 16 by default anyway),

I was generalizing: Notice I used "LEN", not "16". This way, if you have a
string longer than 16 characters, you'll get the StringBuilder of the correct
size, and thus save re-allocation.
and does not add any elements to it.

So?

Look again:
 
C

Chris ( Val )

Chris said:
Also, I'm not sure what you were thinking here, but I
don't think the following is quite correct:
== said:
char[] str = new char[LEN];
// already filled with zeros, so
StringBuilder sb = new StringBuilder( str.length ).append( str );
== </QUOTE FROM EARLIER POST> ==
It looks like you are implying that by providing 'str.length'
to the StringBuilder' constructor, that it will be initialised
with 'zeros' == to 'str.length' before appending 'str'.

Not at all. No implication at all, much less that one.
Ok.
Passing an integer to the StringBuilder constructor only
increases it's [sic: should be "its"] capacity (which is 16 by default anyway),


Yes, you are correct - I got the context confused.
It can happen when you're not concentrating, especially
when using google groups.

I will try to be a little more alert to these kind of
mistakes in the future, but don't let me catch you
making any, because I will let you know about it :)

I was generalizing: Notice I used "LEN", not "16". This way, if you have astringlonger than 16 characters, you'll get the StringBuilder of the correct
size, and thus savere-allocation.

Yes, I did noticed it.

However, in the context of the OP's question, it is
unlikely that a width greater than 16 was required,
given that both examples he provided demonstrated
a width of 5.

So?

Look again:

I have looked, but I think you missunderstood what I stated.

To clarify, the following:

"new StringBuilder( str.length )"

....in itself does not add any elements to sb, and only increases
the capacity - That is all that I was referring to.

In any case, since you have what you want in a char[]
at this point in time, you could have just stuck it
into a "new String( str )", and skipped the use of
the StringBuilder class altogether.
 
L

Lew

Chris said:
I will try to be a little more alert to these kind of
mistakes in the future, but don't let me catch you
making any, because I will let you know about it :)

I welcome your corrections. One should never presume one is infallible.
However, in the context of the OP's question, it is
unlikely that a width greater than 16 was required,
given that both examples he provided demonstrated
a width of 5.
Ok.
I have looked, but I think you missunderstood what I stated.

To clarify, the following:

"new StringBuilder( str.length )"

....in itself does not add any elements to sb, and only increases
the capacity - That is all that I was referring to.

Right, which is why there's an append() there. I never claimed that the
constructor set things to zeros. Why did the point needed stating?
In any case, since you have what you want in a char[]
at this point in time, you could have just stuck it
into a "new String( str )", and skipped the use of
the StringBuilder class altogether.

Excellent point. Thank you.
 
C

Chris ( Val )

Chris ( Val ) wrote:
[snip]
To clarify, the following:
"new StringBuilder( str.length )"
....in itself does not add any elements to sb, and only increases
the capacity - That is all that I was referring to.

Right, which is why there's an append() there. I never claimed that the
constructor set things to zeros. Why did the point needed stating?

[snip]

Like I stated earlier, I was not sure what you
were thinking, because it wasn't all that clear:

<QUOTE>
char[] str = new char[LEN];

// already filled with zeros, so
StringBuilder sb = new StringBuilder( str.length ).append( str );
</QUOTE>

Adding comments to ones code is a good thing, but your
comment appeared to be referring to the StringBuilder
code rather than 'str', which caused me some confusion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top