943,768 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1503
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 17th, 2009
1

String formatting

Expand Post »
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

C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 37
Solved Threads: 10
Light Poster
=OTS=G-Man is offline Offline
47 posts
since Jul 2009
Sep 17th, 2009
0

Re: String formatting

Use:

C# Syntax (Toggle Plain Text)
  1. description = description.Substring(0, 14);

or..

under the console.WriteLine(), display description.SubString(0,14) instead of just description.
Reputation Points: 31
Solved Threads: 21
Junior Poster in Training
jatin24 is offline Offline
74 posts
since Aug 2009
Sep 17th, 2009
2

Re: String formatting

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.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 18th, 2009
0

Re: String formatting

Take a look at MSDN page - Composite Formatting
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 18th, 2009
3

Re: String formatting

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

C# Syntax (Toggle Plain Text)
  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:
text Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 18th, 2009
0

Re: String formatting

Thanks Scott for the nice explanation on formatting issue using Generic and StringBuilder.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 18th, 2009
0

Re: String formatting

And Extension methods brought to you by framework 3.x!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 18th, 2009
0

Re: String formatting

I learnt a lot from this threadm thanks thanks thanks
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Sep 18th, 2009
0

Re: String formatting

Click to Expand / Collapse  Quote originally posted by sknake ...
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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 18th, 2009
0

Re: String formatting

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

Click to Expand / Collapse  Quote originally posted by sknake ...
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.
Reputation Points: 37
Solved Threads: 10
Light Poster
=OTS=G-Man is offline Offline
47 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC