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 2 1700
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)) .PadRight(4);
}
Anders Norås http://dotnetjunkies.com/weblog/anoras/
Thanks.
On Wed, 02 Feb 2005 11:08:09 +0100, Anders Norås
<an**********@o bjectware.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(stri ng str in strings) { output = str.Substring(0 ,(str.Length>=4 ?4:str.Length)) .PadRight(4); }
Anders Norås http://dotnetjunkies.com/weblog/anoras/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 appropriate method from the documentation.
Thanks a lot.
|
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 8 characters
Items are delimited by semicolons or commas. I have to produce a string
delimited only by semicolons and containing items in the first two formats
only. For example,
|
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 alphanums space 8 alphanums
3) 6 alphanums colon 8 alphanums
4) 5 alphanums colon 8 alphanums
My task is to convert items in the third format to the first format, and items
|
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.
..
..
..
|
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 will print literally as {{-Hello}} World, instead of
| |
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 sooooo slow. Anyone know
how I can speed this functions up? I basically need to convert a byte
to string, perform a function on each 'section' of the string, then
reconvert it to a byte. The slow part is the conversion to and from
byte, not the...
|
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
|
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 that string?
I mean, I want the text to lined up as I mentioned earlier - but I dont want the text to be messed up when the formatting is done....
Is there a way todo this?
/Regards, Geir G.
|
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 that
it is because the "mystr" it is already a string.
Anyone can help me?
Sorry my english....
|
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 like to remove the
weekday information that is "Wed" here, and I also would like to
replace the spaces char by "_" and also remove the "\n" char. I didn't
know how to truncate the string from beginning or replace some chars
in a string with another...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |