Hello, I am having the hardest time getting my strings to format right

I want it to only show the first 14 chars of the string. sounds simple, but I think ive been looking at the code to long to see where im messing up :).

here's the code I have

int fCount = mwReader.FieldCount;
            for ( int i = 0; i < fCount; i ++ )
            {
                Console.Write("{0,-15}", mwReader.GetName(i));
                Console.Write(" ");
            }
            Console.WriteLine("".PadLeft(80, '-'));

            while (mwReader.Read())
            {
                string description = mwReader.GetString(0);
                if (description.Length > 14)
                    description.Substring(0, 14);

                Console.WriteLine("{0,-15} {1,-15} {2,-12:C} {3,-12} {4,-12:C}",
                                      description,
                                      mwReader.GetString(1),
                                      mwReader.GetValue(2),
                                      mwReader.GetBoolean(3),
                                      mwReader.GetValue(4)
                                      );
            }

the IF statement was my last attempt to use sub string on it but even that is not working.

Here's the out put im getting

Description     ProductNo       Price           IsOnSale        SalePrice
--------------------------------------------------------------------------------

Heaven's Come To Eearth Today 080689016271    $1.60        False

Christmas Is Jesus 080689015274    $1.60        False
Child Of Light  080689014277    $1.60        False
You Love Me Still 080689008276    $1.60        False
You Are Holy    080689007279    $1.60        False
Worship Only You 080689006272    $1.60        False
Through The Fire 080689005275    $1.60        False
So Much God     080689004278    $1.60        False
Sing To The King 080689003271    $1.60        False
Sing            080689002274    $1.85        False
Praise To The Lord Almighty. 080689001277    $1.60        False

Jude Doxology   080689000270    $1.60        True         $12.95
Berceuse and Finale 00137           $60.00       False
Tell My Father  02501096        $1.80        False

I want it to look like

Description     ProductNo       Price           IsOnSale        SalePrice
--------------------------------------------------------------------------------
Heaven's Come T 080689016271    $1.60        False
Christmas Is Je 080689015274    $1.60        False
Child Of Light  080689014277    $1.60        False
You Love Me Sti 080689008276    $1.60        False
You Are Holy    080689007279    $1.60        False
Worship Only Yo 080689006272    $1.60        False
Through The Fir 080689005275    $1.60        False
So Much God     080689004278    $1.60        False
Sing To The Kin 080689003271    $1.60        False
Sing            080689002274    $1.85        False
Praise To The L 080689001277    $1.60        False
Jude Doxology   080689000270    $1.60        True         $12.95
Berceuse and Fi 00137           $60.00       False
Tell My Father  02501096        $1.80        False

Please Help
and Tank you

Don

serkan sendur commented: i like the way you ask, it is clear +9

Use:

description = description.Substring(0, 14);

or..

under the console.WriteLine(), display description.SubString(0,14) instead of just description. :)

Use Console.WriteLine("{0,14}","Right"); to right align a string and Console.WriteLine("{0,-14}","Left"); left align a string.
Think you have to assign description.Substring(0, 14); to another string.

Edit: Must be getting some sleep : jantin24 already said that...

commented: Yes it is. +17
commented: wooow (Y) +11

I think you may consider abandoning string format because you're getting more in to formatting a line than an individual value.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmStringFormat : Form
  {
    public frmStringFormat()
    {
      InitializeComponent();
    }

    private static string[] GetTestData()
    {
      List<string> result = new List<string>();
      result.Add("Description |    ProductNo    |   Price     |      IsOnSale    |    SalePrice|");
      result.Add("Heaven's Come To Eearth Today| 080689016271 |   $1.60  |      False");
      result.Add("Christmas Is Jesus| 080689015274|    $1.60 |       False|");
      result.Add("Child Of Light|  080689014277 |   $1.60  |      False|");
      result.Add("You Love Me Still| 080689008276|    $1.60 |       False|");
      result.Add("You Are Holy|    080689007279|    $1.60  |      False|");
      result.Add("Worship Only You| 080689006272|    $1.60  |      False|");
      result.Add("Through The Fire| 080689005275|    $1.60 |       False|");
      result.Add("So Much God|     080689004278 |   $1.60  |      False|");
      result.Add("Sing To The King| 080689003271|    $1.60 |       False|");
      result.Add("Sing|            080689002274|    $1.85  |      False|");
      result.Add("Praise To The Lord Almighty.| 080689001277|    $1.60|        False|");
      result.Add("Jude Doxology|   080689000270 |   $1.60  |      True  |       $12.95");
      result.Add("Berceuse and Finale| 00137|           $60.00|       False|");
      result.Add("Tell My Father|  02501096|        $1.80  |      False |");
      return result.ToArray();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      StringBuilder sb = new StringBuilder();
      string[] lines = GetTestData();
      //0= header
      //1+ = values

      const string space = " ";

      for (int i1 = 0; i1 < lines.Length; i1++)
      {
        //0-Desc, 1-Prod#, 2-$$, 3-On Sale, 4-Sale Price
        string[] values = lines[i1].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        sb.Append(values[0].Trim().PadRight(15).Left(15));
        sb.Append(space + values[1].Trim().PadRight(15).Left(15));
        sb.Append(space + values[2].Trim().PadRight(15).Left(15));
        sb.Append(space + values[3].Trim().PadRight(15).Left(15));
        
        if (values.Length >= 5)
          sb.Append(space + values[4].Trim().PadRight(15).Left(15));
          
        sb.AppendLine(string.Empty);
        if (i1 == 0)
        {
          sb.AppendLine(new string('-', sb.Length));
        }
      }
      Console.WriteLine(sb.ToString());
    }
  }
  public static class Extensions
  {
    /// <summary>
    /// Takes the left N characters of a string
    /// </summary>
    /// <param name="str"></param>
    /// <param name="Length">The number of characters to return</param>
    /// <returns></returns>
    public static string Left(this string str, int Length)
    {
      if (string.IsNullOrEmpty(str) || Length.Equals(0))
        return string.Empty;
      else
        return str.Substring(0, Math.Min(Length, str.Length) - 1);
    }
  }
}

Results in:

Description    ProductNo      Price          IsOnSale       SalePrice     
----------------------------------------------------------------------------
Heaven's Come  080689016271   $1.60          False         
Christmas Is J 080689015274   $1.60          False         
Child Of Light 080689014277   $1.60          False         
You Love Me St 080689008276   $1.60          False         
You Are Holy   080689007279   $1.60          False         
Worship Only Y 080689006272   $1.60          False         
Through The Fi 080689005275   $1.60          False         
So Much God    080689004278   $1.60          False         
Sing To The Ki 080689003271   $1.60          False         
Sing           080689002274   $1.85          False         
Praise To The  080689001277   $1.60          False         
Jude Doxology  080689000270   $1.60          True           $12.95        
Berceuse and F 00137          $60.00         False         
Tell My Father 02501096       $1.80          False
commented: Very good explanation indeed. +17
commented: Very nice code... very nice explanation +11
commented: An Introduction to Extension Methods using StringBuilder +2

Thanks Scott for the nice explanation on formatting issue using Generic and StringBuilder.

And Extension methods brought to you by framework 3.x! :)

I learnt a lot from this threadm thanks thanks thanks

And Extension methods brought to you by framework 3.x! :)

I had to +rep you for this example, which was my first exposure to usage of Extension Methods. The fist thing I did of course was drop the code into another class, which produced an error and forced me to read up on the subject a little. ;)

Wow, that you all for the replies, and Glad this topic helped some other people out!

And Extension methods brought to you by framework 3.x! :)

I notice you mention thats from .NET 3.x

I should say, I'm stuck using .NET 2.5 because I only have VS 2005. unless there is a way to get 2005 to use the 3.x compiler?

I was messing around with it after work and ended up just doing what jatin24 mentioned and was what i had originally but my ,and i should have been smarter, ReSharper said was redundant.

You can just remove the "this" in the method and call it like another method. As far as your question about VS2005 I don't know. It has been a while since I have used it :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.