Board index » Visual Studio » String format, minimum width.

String format, minimum width.

Visual Studio92
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


-
 

Re:String format, minimum width.

Quote
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

www.msjogren.net/dotnet/">www.msjogren.net/dotnet/ | www.dotnetinterop.com">www.dotnetinterop.com

Please reply only to the newsgroup.

-

Re:String format, minimum width.

On Nov 5, 8:45 am, "Jameson" <rtgro...@hotmail.com>wrote:

Quote
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



-