473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String length & padding

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
Nov 1 '07 #1
9 7064
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]
- mv*@spam.guard.caspershouse.com

"zion" <zi**@msn.comwrote in message
news:uA*************@TK2MSFTNGP04.phx.gbl...
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

Nov 1 '07 #2
Hi,
You simply needs to calculate the size of the field as a string and then pad
the remaining size of the field

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"zion" <zi**@msn.comwrote in message
news:uA*************@TK2MSFTNGP04.phx.gbl...
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

Nov 1 '07 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eJ**************@TK2MSFTNGP05.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"zion" <zi**@msn.comwrote in message
news:uA*************@TK2MSFTNGP04.phx.gbl...
>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


Nov 1 '07 #4
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
Nov 1 '07 #5
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]
- mv*@spam.guard.caspershouse.com

"zion" <zi**@msn.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eJ**************@TK2MSFTNGP05.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"zion" <zi**@msn.comwrote in message
news:uA*************@TK2MSFTNGP04.phx.gbl...
>>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



Nov 1 '07 #6
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:Oh**************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"zion" <zi**@msn.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eJ**************@TK2MSFTNGP05.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com

"zion" <zi**@msn.comwrote in message
news:uA*************@TK2MSFTNGP04.phx.gbl...
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



Nov 1 '07 #7
zion <zi***@msn.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 1 '07 #8
On Nov 1, 9:44 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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

Nov 2 '07 #9
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
"zion" <zi**@msn.comwrote in message
news:uA*************@TK2MSFTNGP04.phx.gbl...
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

Nov 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mitchua | last post by:
Simplified a bit, I'm parsing HTML documents to get sentences e.g. my $html = get($URL); # remove all HTML TAGs...blah blah blah @sentences = split(/\./, $html)); then I'm trying to determine the...
18
by: Zygmunt Krynicki | last post by:
Hello I've browsed the FAQ but apparently it lacks any questions concenring wide character strings. I'd like to calculate the length of a multibyte string without converting the whole string. ...
8
by: Al Sav | last post by:
Hi, Which one of these below two is a better way to find out if the string is of zero length? string.Length == 0 or string.Length < 0 Alwin S.
10
by: A.M | last post by:
Hi, How can I use validation controls to check max length of string text boxes? Thanks, Alan
3
by: Sam | last post by:
I want to divide character into 2 section. Example, 200412 divided into 2004 in A block and 12 in B block. Please advise how to use left(string, length) or right(string, length) for above...
1
by: MattB | last post by:
Hi. I've got a vb.net/asp.net application that passes strings back a forth between a c++ COM object. We use simple tagging to designate what's what, and it's worked pretty well until now. We had...
3
by: ipellew | last post by:
Hi; Whats the maximum string length in Javascript. Is it the same across all the browsers? max_s_len = str.length // what the max number of chars we can have is str? Regards
5
by: gezerpunta | last post by:
Hi strlen does not return the correct value .I compared the filesize() and strlen byte size but they are not equal. I must find binary string length and it must be equal to filesize() thks.
6
by: csmith8933 | last post by:
I'm trying to loop until the string length is 0, deleting one character at a time. This doesnt work for obvious reasons I'm sure. string s; s = "0000011111"; while (s.length() 0) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.