| | |
String formatting
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 11
Reputation:
Solved Threads: 0
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
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
I want it to look like
Please Help
and Tank you
Don
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)
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
Use:
or..
under the console.WriteLine(), display description.SubString(0,14) instead of just description.
C# Syntax (Toggle Plain Text)
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...
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
Make love, no war. Cave ab homine unius libri.
Danny
Take a look at MSDN page - Composite Formatting
Failure is not fatal, but failure to change might be. - John Wooden
I think you may consider abandoning string format because you're getting more in to formatting a line than an individual value.
Results in:
C# Syntax (Toggle Plain Text)
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:
text Syntax (Toggle Plain Text)
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
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Jul 2009
Posts: 903
Reputation:
Solved Threads: 144
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.
•
•
Join Date: Jul 2009
Posts: 11
Reputation:
Solved Threads: 0
Wow, that you all for the replies, and Glad this topic helped some other people out!
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.
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.
![]() |
Similar Threads
- String Formatting (C#)
- VC++ bloody problem. What to use for string formatting (C++)
- String formatting using the "\t" character (Java)
- Date formatting? (Pascal and Delphi)
Other Threads in the C# Forum
| Thread Tools | Search this Thread |
.net 2008 access activedirectory advice array asp asp.net avltree broadcast c# c++ combobox concurrency connection console control cs4 custom data database datagrid datagridview datastructure datetime dba dbconnection developer development directrobot dll dubai e-commerce editor enabled error excel file form formatting formbox gdi+ httpwebrequest index keypress label linux lisp list listview login mailmerge marshalbyrefobject math mdd mono msword mysql operator path photoshop php pointer post print programming remote remoting reporting resourcefile richtextbox server silverlight softwaredevelopment sql sql-server statistics string stringformatting study tables text textbox totaldays treeview uploadatextfile user validate validation vb video visual-studio visualstudio webdevelopemnt whileloop wia windows winforms wpf write







