String length & padding

  • Thread starter Thread starter zion
  • Start date Start date
Z

zion

Hello,

In my program I get data from table with 2 fields and enter it like in
example into string array.
How can I add the right spaces to the string?
I use the function mystring.Tostring().PadLeft(10,' ') but the result is not
good.


1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks
 
Hi,


You simply needs to calculate the size of the field as a string and then pad
the remaining size of the field
 
zion,

PadLeft will do exactly what you want it to do, add a number of spaces
to the left of the string.

What you want, I believe, is to only add the number of spaces so that
the length of the strings is consistent.

So, if you want a string with 10 characters, and the string you have is
five characters, then you will want to add five more space characters.

You basically have to subtract the length of the string from the desired
length, and that is what you will pass to PadLeft.
 
Hi,

As I told you, I use the function PadLeft(10,' ') but result is not the
expected.
Only if I use PadLeft(10,'0') then the result is OK.
The conclusion is that space is not the same like zero.
Do you have another idea?

Thanks




Nicholas Paldino said:
zion,

PadLeft will do exactly what you want it to do, add a number of spaces
to the left of the string.

What you want, I believe, is to only add the number of spaces so that
the length of the strings is consistent.

So, if you want a string with 10 characters, and the string you have is
five characters, then you will want to add five more space characters.

You basically have to subtract the length of the string from the
desired length, and that is what you will pass to PadLeft.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

zion said:
Hello,

In my program I get data from table with 2 fields and enter it like in
example into string array.
How can I add the right spaces to the string?
I use the function mystring.Tostring().PadLeft(10,' ') but the result is
not good.


1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks
 
zion brought next idea :
Hello,

In my program I get data from table with 2 fields and enter it like in
example into string array.
How can I add the right spaces to the string?
I use the function mystring.Tostring().PadLeft(10,' ') but the result is not
good.


1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks

If you want to format a message in this way, be sure to use a
fixed-space font such as Courier New.
Try (in your original font) to pad with lowercase L (l) or uppercase W
- the results are different again.
A space is probably smaller than a digit. I think the digits do have
the same with, but most letters have a different width.

Hans Kesting
 
This is a display issue? If that is the case, then you have to use a
monospaced font so that spaces take up the same amount of space as other
characters.

If you don't want to use a monospaced font, then you will need to draw
the string yourself, measuring the space that a '0' takes up, and then
adjusting where you draw the number manually.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

zion said:
Hi,

As I told you, I use the function PadLeft(10,' ') but result is not the
expected.
Only if I use PadLeft(10,'0') then the result is OK.
The conclusion is that space is not the same like zero.
Do you have another idea?

Thanks




Nicholas Paldino said:
zion,

PadLeft will do exactly what you want it to do, add a number of spaces
to the left of the string.

What you want, I believe, is to only add the number of spaces so that
the length of the strings is consistent.

So, if you want a string with 10 characters, and the string you have
is five characters, then you will want to add five more space characters.

You basically have to subtract the length of the string from the
desired length, and that is what you will pass to PadLeft.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

zion said:
Hello,

In my program I get data from table with 2 fields and enter it like in
example into string array.
How can I add the right spaces to the string?
I use the function mystring.Tostring().PadLeft(10,' ') but the result is
not good.


1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks
 
Monospaced font is not a good idea.
How can I draw the string? My application is asp.net with Ajax
AutoCompleteExtender and webservice for the data.



Nicholas Paldino said:
This is a display issue? If that is the case, then you have to use a
monospaced font so that spaces take up the same amount of space as other
characters.

If you don't want to use a monospaced font, then you will need to draw
the string yourself, measuring the space that a '0' takes up, and then
adjusting where you draw the number manually.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

zion said:
Hi,

As I told you, I use the function PadLeft(10,' ') but result is not the
expected.
Only if I use PadLeft(10,'0') then the result is OK.
The conclusion is that space is not the same like zero.
Do you have another idea?

Thanks




Nicholas Paldino said:
zion,

PadLeft will do exactly what you want it to do, add a number of
spaces to the left of the string.

What you want, I believe, is to only add the number of spaces so that
the length of the strings is consistent.

So, if you want a string with 10 characters, and the string you have
is five characters, then you will want to add five more space
characters.

You basically have to subtract the length of the string from the
desired length, and that is what you will pass to PadLeft.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

In my program I get data from table with 2 fields and enter it like in
example into string array.
How can I add the right spaces to the string?
I use the function mystring.Tostring().PadLeft(10,' ') but the result
is not good.


1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks
 
zion said:
Monospaced font is not a good idea.

It's the only way of making sure things line up if all you've got is
text.
How can I draw the string? My application is asp.net with Ajax
AutoCompleteExtender and webservice for the data.

Then you should be using an HTML table. That's how things are meant to
be lined up in HTML.
 
So, if you want a string with 10 characters, and the string you have is
five characters, then you will want to add five more space characters.

Actually, the value you pass into PadLeft (or PadRight) is the *total*
length of the string. So if you want a string 10 characters, you
would pass 10 to the PadLeft method. However, the method will not
truncate the string. If you pass 10 into PadLeft but your string is
already more than 10 in length, it will not be truncated down to 10.

Chris
 
You're probably looking for something like:

SizeF size = grfx.MeasureString (font, str);

int strWidth = (int) size.Width;

grfx.DrawString (font, str, rightMostX - width);

I'm sure my syntax is all wrong, but basically figure out the far right X
and draw the string 'x' pixels to the left of that where 'x' is the width of
the string you're drawing.

Hilton
 

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

Back
Top