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

Home Posts Topics Members FAQ

String.Format() with max chars?

MC
Is it possible to use String.Format and a format specifier in such a
way to ensure that a string does not exceed the field width in which
it is placed?

Example:

string [] strings = {"Hello World", "Wednesday Evening", "Hi"};
string output;

foreach(string str in strings)
{
output = String.Format("{0,-4:???}", str);
}

The -4 ensures that str is placed into a field width 4 characters
wide, left justified--great!

What does ??? have to be so that str never exceeds 4 characters--so
that the output is:

Hell
Wedn
Hi

This can be done in C with printf(), so I thought it would be possible
in .NET.

Thanks,
Craig
Jul 21 '05 #1
2 1686
MC wrote:
Is it possible to use String.Format and a format specifier in such a
way to ensure that a string does not exceed the field width in which
it is placed?

There is no built-in IFormatProvider for this, but you could build one
yourself. There are easier solutions available, the code below creates
fixed length strings.

foreach(string str in strings)
{
output = str.Substring(0,(str.Length>=4?4:str.Length)).PadR ight(4);
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Jul 21 '05 #2
MC
Thanks.

On Wed, 02 Feb 2005 11:08:09 +0100, Anders Norås
<an**********@objectware.no> wrote:
MC wrote:
Is it possible to use String.Format and a format specifier in such a
way to ensure that a string does not exceed the field width in which
it is placed?

There is no built-in IFormatProvider for this, but you could build one
yourself. There are easier solutions available, the code below creates
fixed length strings.

foreach(string str in strings)
{
output = str.Substring(0,(str.Length>=4?4:str.Length)).PadR ight(4);
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/


Jul 21 '05 #3

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

Similar topics

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...
6
8310
by: Christopher Benson-Manica | last post by:
I have a C-style string (null-terminated) that consists of items in one of the following formats: 14 characters 5 characters space 8 characters 6 characters colon 8 characters 5 characters colon...
16
5957
by: Christopher Benson-Manica | last post by:
I'm wondering about the best way to do the following: I have a string delimited by semicolons. The items delimited may be in any of the following formats: 1) 14 alphanum characters 2) 5...
6
5698
by: Kifah Abbad | last post by:
Ok guys, here is the deal, i hope someone can help me out with this. I receive a string through a socket...i dunno how long this string is, but i save it all into a buf. .. .. ..
23
9454
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
16
2122
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
1
912
by: John | last post by:
Hi How can I format a string in format xxx xxxx xxxx ie 3 chars then space then 4 chars then space then 4 chars? This should work even if there are insufficient characters. Thanks regards
12
2081
by: n00bish | last post by:
Say I have a string with a length of 30 chars, and I want to re-format that string into lines with each a maximum of 10 chars. How do I format that text without changing the sentences involved in...
8
2195
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
13
3714
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
7132
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
7009
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
7178
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
7223
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
6899
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
7390
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
4602
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
1427
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 ...
0
302
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...

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.