473,379 Members | 1,423 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,379 software developers and data experts.

Need Help on String.Format() method

hi guys!
back again with another query.
the problem is like this.

i want to print a line like this:
"---------------------------------------------"

the easiest way is to simply assign it to string and print it.
but i want to use the String.Format() method if possible to do it.

All i came to know about this method is, you can use
String.Format("{argumentIndex[,alignment][:formatString]}",argumentIndex)

do specify paddin and some format. but i dont get it how to provide the
formatstring. i guess it is for formating the argument if i'm not
wrong.
so guys, i need your help here.
i want to do something like this
String.Format("{0,40}","-")

this way it should return me the string filled with 40 dashes.

if there is any better way to do it, please let me know.
i'm trying to learn something about String object. Please help me.

Thanks,

Lucky

Nov 24 '06 #1
8 4969
"Lucky" <tu************@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
if there is any better way to do it, please let me know.
string strRepeat = new String('-', 40);
i'm trying to learn something about String object. Please help me.
Google
MSDN
Nov 24 '06 #2
You're barking up the wrong tree here, Lucky.

The String Object does not implement IFormattable, because an instance of
System.String is already a string. Therefore, you can not use String.Format
to format a string. The String.Format method replaces the "format item(s)"
represented by the substring(s) having curly brackets in the string, with
Formatted text representations of the objects that are in the parameter or
parameters (depending on the overload you use) following the string
parameter. There is one overload that takes an IFormatProvider as the first
argument, enabling you to create a custom IFormatter class to use as the
Formatter for the object(s) represented by the parameter(s) following the
string parameter.

It is a way of creating a string that contains data that is not of the
string type, without having to do a lot of formatting and concatenation to
do it. For example, let's say that you have a DateTime variable, and you
want to put the DateTime variable into a user-friendly string message in a
label on a Form. You could do something like the following:

DateTime d = DateTime.Now;
string s = "Today's date is ";
s += d.ToString("dd/MM/yyyy");

Using the static String.Format method, you can simply use:

string s = String.Format("Today's date is {0:dd/MM/yyyy}, DateTime.Now);

In other words, if you're really doing this to understand System.String, the
only thing you will learn about System.String from this is how the
String.Format method works, *not* how to use the String.Format method to
create a string containing a series of identical characters.

For more information, see:

http://msdn2.microsoft.com/en-us/library/fbxft59x.aspx
http://msdn2.microsoft.com/en-us/library/26etazsy.aspx
http://msdn2.microsoft.com/en-us/lib...em.string.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
"Lucky" <tu************@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
hi guys!
back again with another query.
the problem is like this.

i want to print a line like this:
"---------------------------------------------"

the easiest way is to simply assign it to string and print it.
but i want to use the String.Format() method if possible to do it.

All i came to know about this method is, you can use
String.Format("{argumentIndex[,alignment][:formatString]}",argumentIndex)

do specify paddin and some format. but i dont get it how to provide the
formatstring. i guess it is for formating the argument if i'm not
wrong.
so guys, i need your help here.
i want to do something like this
String.Format("{0,40}","-")

this way it should return me the string filled with 40 dashes.

if there is any better way to do it, please let me know.
i'm trying to learn something about String object. Please help me.

Thanks,

Lucky

Nov 24 '06 #3
Please explain what IFormattable is i don't understand most of this,
and too am trying to learn as much as I can.

Thankyou

Nov 24 '06 #4
Hello Lucky,

I dont think with string.Format() you can get the output what you want.

Might be following page would be useful..
http://idunno.org/displayBlog.aspx/2004071401

You can pad the string with space with format function but repeating
the same character, am not sure !!

Regards,
Jigar mehta

Lucky wrote:
hi guys!
back again with another query.
the problem is like this.

i want to print a line like this:
"---------------------------------------------"

the easiest way is to simply assign it to string and print it.
but i want to use the String.Format() method if possible to do it.

All i came to know about this method is, you can use
String.Format("{argumentIndex[,alignment][:formatString]}",argumentIndex)

do specify paddin and some format. but i dont get it how to provide the
formatstring. i guess it is for formating the argument if i'm not
wrong.
so guys, i need your help here.
i want to do something like this
String.Format("{0,40}","-")

this way it should return me the string filled with 40 dashes.

if there is any better way to do it, please let me know.
i'm trying to learn something about String object. Please help me.

Thanks,

Lucky
Nov 24 '06 #5
ga********@myway.com wrote:
Please explain what IFormattable is i don't understand most of this,
and too am trying to learn as much as I can.
If you look up IFormattable in MSDN, there's lot of information there.

Jon

Nov 24 '06 #6
If you look at the 3 links I posted, the IFormattable interface, as well as
everything else I referred to, is explained in those references.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
<ga********@myway.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
Please explain what IFormattable is i don't understand most of this,
and too am trying to learn as much as I can.

Thankyou

Nov 24 '06 #7
* Lucky wrote, On 24-11-2006 13:33:
hi guys!
back again with another query.
the problem is like this.

i want to print a line like this:
"---------------------------------------------"

the easiest way is to simply assign it to string and print it.
but i want to use the String.Format() method if possible to do it.

All i came to know about this method is, you can use
String.Format("{argumentIndex[,alignment][:formatString]}",argumentIndex)

do specify paddin and some format. but i dont get it how to provide the
formatstring. i guess it is for formating the argument if i'm not
wrong.
so guys, i need your help here.
i want to do something like this
String.Format("{0,40}","-")

this way it should return me the string filled with 40 dashes.

if there is any better way to do it, please let me know.
i'm trying to learn something about String object. Please help me.
Lucy,

You could try:

string dashes = string.Empty.PadLeft(40,'-');

Jesse
Nov 24 '06 #8
I must say, very helping people around here. thanks everybody for your
informative replies. i learnt lot from your replies but i must
appriciate the replay from Google MSDN which was to the point.

Lucky

Mark Rae wrote:
"Lucky" <tu************@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
if there is any better way to do it, please let me know.

string strRepeat = new String('-', 40);
i'm trying to learn something about String object. Please help me.

Google
MSDN
Dec 1 '06 #9

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

Similar topics

5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
15
by: Fariba | last post by:
Hello , I am trying to call a mthod with the following signature: AddRole(string Group_Nam, string Description, int permissionmask); Accroding to msdn ,you can mask the permissions using...
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
3
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return...
1
by: twin2003 | last post by:
need help with inventory part 5 here is what I have to do Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: Dale | last post by:
Access 2000 I have a check scanner from Magtek, (MicrImage) This is like a "point of sale" check scanner/credit card reader. Magtek furnished 2 ocx's (MTMicrImage.ocx & SaxComm8.ocx). They also...
1
by: Arthur Dent | last post by:
I was wondering, ... does anyone know off hand which is faster? I've seen articles where people suggest the method of using XML literals to build strings... something like: Dim s As String =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.