473,657 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Tostri ng().PadLeft(10 ,' ') but the result is not
good.
1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks
Nov 1 '07 #1
9 7080
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.co m

"zion" <zi**@msn.comwr ote in message
news:uA******** *****@TK2MSFTNG P04.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.Tostri ng().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.comwr ote in message
news:uA******** *****@TK2MSFTNG P04.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.Tostri ng().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.c omwrote in
message news:eJ******** ******@TK2MSFTN GP05.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.co m

"zion" <zi**@msn.comwr ote in message
news:uA******** *****@TK2MSFTNG P04.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.Tostri ng().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.Tostri ng().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.co m

"zion" <zi**@msn.comwr ote in message
news:%2******** ********@TK2MSF TNGP06.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.c omwrote
in message news:eJ******** ******@TK2MSFTN GP05.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.co m

"zion" <zi**@msn.comwr ote in message
news:uA******* ******@TK2MSFTN GP04.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.Tostri ng().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
AutoCompleteExt ender and webservice for the data.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:Oh******** ******@TK2MSFTN GP06.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.co m

"zion" <zi**@msn.comwr ote in message
news:%2******** ********@TK2MSF TNGP06.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.c omwrote
in message news:eJ******** ******@TK2MSFTN GP05.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.co m

"zion" <zi**@msn.comwr ote in message
news:uA****** *******@TK2MSFT NGP04.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.Tostri ng().PadLeft(10 ,' ') but the result
is not good.
1234567890 | ABCDEFG
12345 | ABC
12 | A

Thanks



Nov 1 '07 #7
zion <zi***@msn.comw rote:
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
AutoCompleteExt ender 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.co m>
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.guar d.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.MeasureStr ing (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.comwr ote in message
news:uA******** *****@TK2MSFTNG P04.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.Tostri ng().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
14620
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 number of characters in the sentence. However, although when I print the sentences they look fine, when I use length($sentence) I get values in the hundreds for small sentences. Most documentation I found said "length() returns the number of...
18
5605
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. Zygmunt PS: The whole multibyte string vs wide character string concept is broken IMHO since it allows wchar_t not to be large enough to contain a full character (rendering both types virtually the same). What's the point of
8
1799
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
9487
by: A.M | last post by:
Hi, How can I use validation controls to check max length of string text boxes? Thanks, Alan
3
6091
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 request. Many thanks in advance.
1
1302
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 some data that wasn't parsing right because it also contained tags that conflicted with the tags designating the data. To get around this the programmer of the COM object (not me) decided we could put string lengths in the tags, so if an element...
3
7412
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
28443
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
6133
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) { functionOne(s); }
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.