473,563 Members | 2,662 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.PadLef t ( ...... )

?

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

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

myString = myString.PadLef t(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...@hotma il.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.PadLef t ( ...... )

?

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
3277
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 ' 2' Now, with a mapping....
12
16082
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 type. Now the problem is how to print the binary format of that charater to the standard output. It seems a common task but I just cannot find the...
2
15954
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++ as follows: C - printf("%5d", someInt); C++ - cout << setw(5) << someInt;
7
1682
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: Float String 1.0 1
4
3750
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 having to use concatenation, which is awkward to write and to read: s.Format("{0," + fieldWidth.ToString() + "}", val); Preferably, I'd like to...
2
8198
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); Pads myFName, width of 10 minimum I am used to printf which allows a '*' to be used to specify a variable to be used at runtime to specify the...
1
6085
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 TEMPLATE1 (data matrix) ==>A region of interest from an image. Y_min & Y_max = minimum and maximum of the height value. XL & XR = min and max of the...
2
3918
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 contains a listbox and the second column contains some buttons. I am trying to display it such that if the listbox is empty it will be at least...
3
1664
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 fields and tabulate them, so each field is in a column, in a single String, one line per object with \n separating them, calculating the minimum...
0
7580
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...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7634
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...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
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...

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.