473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1682
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...

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.