473,499 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String format, minimum width.

Hi,

Dumb question for you:

Is there a quick and easy way to ensure a string has a minimum width using
format specifier?

Should I just write

if myString.Length < 8 then myString.PadLeft ( ...... )

?

Thanks
Nov 5 '07 #1
2 1941
>Should I just write
>
if myString.Length < 8 then myString.PadLeft ( ...... )

?
No need to check the length first, PadLEft does that for you. Just do

myString = myString.PadLeft(8)

What do you mean by format specifier? Do you get myString from a call
to String.Format or some other formatting method?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 5 '07 #2
On Nov 5, 8:45 am, "Jameson" <rtgro...@hotmail.comwrote:
Hi,

Dumb question for you:

Is there a quick and easy way to ensure a string has a minimum width using
format specifier?

Should I just write

if myString.Length < 8 then myString.PadLeft ( ...... )

?

Thanks
In case you want to use string.format...

dim sMessage as string = string.format("{0,-8}", value)

If value is less then 8 characters, the result will be an 8 character
string left padded with spaces. To right pad, make the value after
the comma a positive number.

HTH

--
Tom Shelton

Nov 6 '07 #3

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

Similar topics

20
3263
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' ...
12
16073
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string...
2
15935
by: Kevin | last post by:
I am looking for a way in C# to format my output. I want to be able to specify a disply width and then have the variable printed at minimum with that width. I can accomplish this is C and C++...
7
1676
by: Steven D'Aprano | last post by:
I have a sinking feeling I'm missing something really, really simple. I'm looking for a format string similar to '%.3f' except that trailing zeroes are not included. To give some examples: ...
4
3741
by: MC | last post by:
In C, printf allows us to specify a runtime (non-constant) field width of a formatted value by using the * character. Is there something like that for System.String.Format? In C#, I find myself...
2
8192
by: tstephan | last post by:
In the docs for String.Format there is an alignment component which allows you to pad the field to a certain number of spaces. e.g. FormatFName = String.Format("First Name = |{0,10}|", myFName);...
1
6071
by: TommyC | last post by:
Hi all, I fail to write the image data to binary PGM format (P5). Let's say i have to write a image data into PGM P5 format. I have the information for width, height, Y_min, Y_max, XL, XR and the...
2
3905
by: cshaw | last post by:
Hello Everyone, I am having problems with a listbox control. I have a page with a couple of labels and drop-down lists at the top, and then below there is a table with two columns, the first column...
3
1655
by: kelvin.koogan | last post by:
Using C++/CLI but I would imagine C# is the same. What is the fastest to do the following operations 1) Compare two Strings without case-sensitivity. 2) Take a group of objects with 4 string...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
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
7174
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
7220
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...
1
6894
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
7388
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...
0
4600
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...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.