String.format???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anders M

    String.format???

    Hi!

    I'd like to format a string as follows:

    hi my name is anders and I'm programming
    to:
    hi my name...

    The formatting should shorten the text to at most 10 chars and add "..." in
    the end.

    Is that possible with String.Format?

    /Anders



  • emr

    #2
    Re: String.format?? ?

    string text,text2;
    text=textbox1.t ext;
    text2=text.Subs tring(0,10) + "...";

    hope this helps


    Comment

    • Bob Powell [MVP]

      #3
      Re: String.format?? ?

      The elipses will be placed automatically if you use the
      StringFormat.Tr imming property and the StringTrimming EllipsisCharact er
      enumeration. The string trimming happens when you try and output a string
      into a rectangle that's too small for the string. Use DrawString and specify
      a bounding rectangle for the string.


      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.





      "Anders M" <it99ama@du.s e> wrote in message
      news:eYVH4BYSFH A.3076@tk2msftn gp13.phx.gbl...[color=blue]
      > Hi!
      >
      > I'd like to format a string as follows:
      >
      > hi my name is anders and I'm programming
      > to:
      > hi my name...
      >
      > The formatting should shorten the text to at most 10 chars and add "..."
      > in
      > the end.
      >
      > Is that possible with String.Format?
      >
      > /Anders
      >
      >
      >[/color]


      Comment

      • Bruce Wood

        #4
        Re: String.format?? ?

        Well, not quite. The quick 'n' dirty solution would be:

        string trimmedText;
        if (text.Length > 10)
        {
        trimmedText = text.Substring( 0, 10) + "...";
        }
        else
        {
        trimmedText = text;
        }

        or, if you want to be _really_ quick 'n' dirty, you could use the
        ternary operator (ewww):

        string trimmedText = text.Length > 10 : text.Substring( 0,10) + "..." :
        text;

        Comment

        Working...