Need Help on String.Format() method

Discussion in 'ASP .Net' started by Lucky, Nov 24, 2006.

  1. Lucky

    Lucky Guest

    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
     
    Lucky, Nov 24, 2006
    #1
    1. Advertising

  2. Lucky

    Mark Rae Guest

    "Lucky" <> wrote in message
    news:...

    > 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
     
    Mark Rae, Nov 24, 2006
    #2
    1. Advertising

  3. 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/library/system.string.aspx

    --
    HTH,

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

    Never trust a dunderhead with a blunderbuss.


    "Lucky" <> wrote in message
    news:...
    > 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
    >
     
    Kevin Spencer, Nov 24, 2006
    #3
  4. Lucky

    Guest

    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, 2006
    #4
  5. 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
     
    Jigar.programmer, Nov 24, 2006
    #5
  6. 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
     
    Jon Skeet [C# MVP], Nov 24, 2006
    #6
  7. 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.


    <> wrote in message
    news:...
    > Please explain what IFormattable is i don't understand most of this,
    > and too am trying to learn as much as I can.
    >
    > Thankyou
    >
     
    Kevin Spencer, Nov 24, 2006
    #7
  8. * 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
     
    Jesse Houwing, Nov 24, 2006
    #8
  9. Lucky

    Lucky Guest

    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" <> wrote in message
    > news:...
    >
    > > 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
     
    Lucky, Dec 1, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,297
    Roedy Green
    May 10, 2004
  2. ankur
    Replies:
    1
    Views:
    12,891
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  3. Chris Angelico
    Replies:
    3
    Views:
    160
    Mark Lawrence
    Mar 1, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    138
    Peter Otten
    Feb 28, 2013
  5. Rick Johnson
    Replies:
    0
    Views:
    146
    Rick Johnson
    Feb 28, 2013
Loading...

Share This Page