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,
5 3012
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... - string.Format("{0,-10}--", "test");
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");
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
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
|
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
' ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 #...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |