473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Formatting a string to print out

Hello,

I face one problem in printing out the strings.
I show the example for easy reference.
For the output results, I attached the text file because this page cannot show my example outputs that I want to mean.

My Code

string str = "";
string[] fruit ={ "Orange", "Apple", "WaterMelon" };

for (int i = 0; i < fruit.Length; i++)
{
str += string.Format("{0} {1,5} {2,5} {3,5}" + Environment.NewLine,
fruit[i], "1", "2", "4");

}

Console.WriteLine(str);

For output, see my attachment.


So, please help me.

And , for another condition,
if the data to print are taken from the datarows in datatable, how can I write the code to get the output that I want.

Thanks,
Attached Files
File Type: txt Output_Examples.txt (248 Bytes, 433 views)
Jan 8 '10 #1
5 3012
GaryTexmo
1,501 Expert 1GB
You need to format the fruit text as well... I went through MSDN stuff to find out how. Here's the link I looked at...

http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

Basically, you need to pad the right of your fruit text with spaces to make it a fixed width. You're already padding the left of your number text, so you know how to pad the fruit text. Just use a negative number to pad right instead of left (so weird!).

For example...
Expand|Select|Wrap|Line Numbers
  1. string.Format("{0,-10}--", "test");
Jan 8 '10 #2
tlhintoq
3,525 Expert 2GB
I could also suggest using a tab ( \t ) instead of hard coding the spaces. This would make it more flexible when imported to spreadsheets and word processors.
Excel for example will recognize the tab as a column break.

str += string.Format("{0}\t{1}\t{2}\t{3}\n",fruit[i], "1", "2", "4");
Jan 8 '10 #3
I really feel glad of using this web site and this program.
Thanks a lot for both quick answers.

Both are very useful and hit my target.

But, in using tab(\t), I face some problem.
Because, in my example,
I think, the length of "Watermelon" takes one tab.
So, the last line differs one tab from the upper two lines.

So, how can I adjust ?

Thanks
Jan 9 '10 #4
I really feel glad of using this web site and this program.
Thanks a lot for both quick answers.

Both are very useful and hit my target.

But, in using tab(\t), I face some problem.
Because, in my example,
I think, the length of "Watermelon" takes one tab.
So, the last line differs one tab from the upper two lines.

So, how can I adjust ?

Thanks
Jan 9 '10 #5
GaryTexmo
1,501 Expert 1GB
You can't, really, that's why you pad the string. I think Thlintoq was suggesting using a tab space to format for output in a spreadsheet, which you can do, but if you want to align columns in the text display you'd output it differently.

If you just want the text display, pad the strings.
Jan 9 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Pascal | last post by:
Hello, I've a float number 123456789.01 and, I'de like to format it like this "123 456 789.01". Is this possible with % character?
20
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' ...
6
by: Brian | last post by:
Hello all, I am a bit stuck with a float formatting issue. What I want to do is print a float to the screen with each line showing one more decimal place. Here is a code snip that may explain...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
9
by: sck10 | last post by:
Hello, I have a page with an ImageButton that is used to redirect to another page. When the page first opens, everything looks as expected. However, when I click on the image, the new page...
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
3
by: preethi303 | last post by:
Hi, I'm using VB 2005 and I have a problem formatting a string when I use the print preview dialog box. I'm using just the vbCrLf character and the Space function to correctly align my data and...
2
by: Tim Chase | last post by:
Is there an easy way to make string-formatting smart enough to gracefully handle iterators/generators? E.g. transform = lambda s: s.upper() pair = ('hello', 'world') print "%s, %s" % pair #...
3
by: Tim | last post by:
Folks, I'm trying to format a print string so that it reports progress whilst processing a looping structure with a date time stamp appended to the end of the string. This started out life as a...
30
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.