String formatting

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 11
Reputation: =OTS=G-Man is an unknown quantity at this point 
Solved Threads: 0
=OTS=G-Man =OTS=G-Man is offline Offline
Newbie Poster

String formatting

 
1
  #1
Sep 17th, 2009
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

  1. int fCount = mwReader.FieldCount;
  2. for ( int i = 0; i < fCount; i ++ )
  3. {
  4. Console.Write("{0,-15}", mwReader.GetName(i));
  5. Console.Write(" ");
  6. }
  7. Console.WriteLine("".PadLeft(80, '-'));
  8.  
  9. while (mwReader.Read())
  10. {
  11. string description = mwReader.GetString(0);
  12. if (description.Length > 14)
  13. description.Substring(0, 14);
  14.  
  15. Console.WriteLine("{0,-15} {1,-15} {2,-12:C} {3,-12} {4,-12:C}",
  16. description,
  17. mwReader.GetString(1),
  18. mwReader.GetValue(2),
  19. mwReader.GetBoolean(3),
  20. mwReader.GetValue(4)
  21. );
  22. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 70
Reputation: jatin24 is an unknown quantity at this point 
Solved Threads: 19
jatin24's Avatar
jatin24 jatin24 is offline Offline
Junior Poster in Training

Re: String formatting

 
0
  #2
Sep 17th, 2009
Use:

  1. description = description.Substring(0, 14);

or..

under the console.WriteLine(), display description.SubString(0,14) instead of just description.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,918
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: String formatting

 
2
  #3
Sep 17th, 2009
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...
Last edited by ddanbe; Sep 17th, 2009 at 10:39 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 462
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: String formatting

 
0
  #4
Sep 18th, 2009
Take a look at MSDN page - Composite Formatting
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: String formatting

 
3
  #5
Sep 18th, 2009
I think you may consider abandoning string format because you're getting more in to formatting a line than an individual value.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb
  11. {
  12. public partial class frmStringFormat : Form
  13. {
  14. public frmStringFormat()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private static string[] GetTestData()
  20. {
  21. List<string> result = new List<string>();
  22. result.Add("Description | ProductNo | Price | IsOnSale | SalePrice|");
  23. result.Add("Heaven's Come To Eearth Today| 080689016271 | $1.60 | False");
  24. result.Add("Christmas Is Jesus| 080689015274| $1.60 | False|");
  25. result.Add("Child Of Light| 080689014277 | $1.60 | False|");
  26. result.Add("You Love Me Still| 080689008276| $1.60 | False|");
  27. result.Add("You Are Holy| 080689007279| $1.60 | False|");
  28. result.Add("Worship Only You| 080689006272| $1.60 | False|");
  29. result.Add("Through The Fire| 080689005275| $1.60 | False|");
  30. result.Add("So Much God| 080689004278 | $1.60 | False|");
  31. result.Add("Sing To The King| 080689003271| $1.60 | False|");
  32. result.Add("Sing| 080689002274| $1.85 | False|");
  33. result.Add("Praise To The Lord Almighty.| 080689001277| $1.60| False|");
  34. result.Add("Jude Doxology| 080689000270 | $1.60 | True | $12.95");
  35. result.Add("Berceuse and Finale| 00137| $60.00| False|");
  36. result.Add("Tell My Father| 02501096| $1.80 | False |");
  37. return result.ToArray();
  38. }
  39.  
  40. private void button1_Click(object sender, EventArgs e)
  41. {
  42. StringBuilder sb = new StringBuilder();
  43. string[] lines = GetTestData();
  44. //0= header
  45. //1+ = values
  46.  
  47. const string space = " ";
  48.  
  49. for (int i1 = 0; i1 < lines.Length; i1++)
  50. {
  51. //0-Desc, 1-Prod#, 2-$$, 3-On Sale, 4-Sale Price
  52. string[] values = lines[i1].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  53. sb.Append(values[0].Trim().PadRight(15).Left(15));
  54. sb.Append(space + values[1].Trim().PadRight(15).Left(15));
  55. sb.Append(space + values[2].Trim().PadRight(15).Left(15));
  56. sb.Append(space + values[3].Trim().PadRight(15).Left(15));
  57.  
  58. if (values.Length >= 5)
  59. sb.Append(space + values[4].Trim().PadRight(15).Left(15));
  60.  
  61. sb.AppendLine(string.Empty);
  62. if (i1 == 0)
  63. {
  64. sb.AppendLine(new string('-', sb.Length));
  65. }
  66. }
  67. Console.WriteLine(sb.ToString());
  68. }
  69. }
  70. public static class Extensions
  71. {
  72. /// <summary>
  73. /// Takes the left N characters of a string
  74. /// </summary>
  75. /// <param name="str"></param>
  76. /// <param name="Length">The number of characters to return</param>
  77. /// <returns></returns>
  78. public static string Left(this string str, int Length)
  79. {
  80. if (string.IsNullOrEmpty(str) || Length.Equals(0))
  81. return string.Empty;
  82. else
  83. return str.Substring(0, Math.Min(Length, str.Length) - 1);
  84. }
  85. }
  86. }

Results in:
  1. Description ProductNo Price IsOnSale SalePrice
  2. ----------------------------------------------------------------------------
  3. Heaven's Come 080689016271 $1.60 False
  4. Christmas Is J 080689015274 $1.60 False
  5. Child Of Light 080689014277 $1.60 False
  6. You Love Me St 080689008276 $1.60 False
  7. You Are Holy 080689007279 $1.60 False
  8. Worship Only Y 080689006272 $1.60 False
  9. Through The Fi 080689005275 $1.60 False
  10. So Much God 080689004278 $1.60 False
  11. Sing To The Ki 080689003271 $1.60 False
  12. Sing 080689002274 $1.85 False
  13. Praise To The 080689001277 $1.60 False
  14. Jude Doxology 080689000270 $1.60 True $12.95
  15. Berceuse and F 00137 $60.00 False
  16. Tell My Father 02501096 $1.80 False
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 462
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: String formatting

 
0
  #6
Sep 18th, 2009
Thanks Scott for the nice explanation on formatting issue using Generic and StringBuilder.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: String formatting

 
0
  #7
Sep 18th, 2009
And Extension methods brought to you by framework 3.x!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: String formatting

 
0
  #8
Sep 18th, 2009
I learnt a lot from this threadm thanks thanks thanks
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: String formatting

 
0
  #9
Sep 18th, 2009
Originally Posted by sknake View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 11
Reputation: =OTS=G-Man is an unknown quantity at this point 
Solved Threads: 0
=OTS=G-Man =OTS=G-Man is offline Offline
Newbie Poster

Re: String formatting

 
0
  #10
Sep 18th, 2009
Wow, that you all for the replies, and Glad this topic helped some other people out!

Originally Posted by sknake View Post
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.
Reply With Quote Quick reply to this message  
Reply

Tags
c#, stringformatting

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC