473,471 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String Alignment Problem

Friends,

My Problem is related to writing a text file from a database
using .Net. in a particular string format.
I have a dataset with a table, let suppose in this table there are
three fields.
- Code
- Description
- Amount

At the time of writing a text file I use in a for loop -

String.Format("{0,10}{1, 40} {2,-10}", DS.Table(0).Row(i)
("Code").ToString(), DS.Table(0).Row(i)("Description").ToString(),
DS.Table(0).Row(i)("Amount").ToString())
But the problem is related to the alignment. Data comes like in
following format:

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
I want Data comes in following proper alignment format.

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
So anybody knows the solutions of this problem, please and please
reply imediately, its urgent.

Rahul

Jun 4 '07 #1
5 4382
Rahul wrote:
Friends,

My Problem is related to writing a text file from a database
using .Net. in a particular string format.
I have a dataset with a table, let suppose in this table there are
three fields.
- Code
- Description
- Amount

At the time of writing a text file I use in a for loop -

String.Format("{0,10}{1, 40} {2,-10}", DS.Table(0).Row(i)
("Code").ToString(), DS.Table(0).Row(i)("Description").ToString(),
DS.Table(0).Row(i)("Amount").ToString())
String.Format("{0,10}{1, 40} {2,-10}",
does not cut to 10,40 and 10 places - if the content is longer it will
puzzle your output.

For testing I would insert a delimiter:
String.Format("{0,10}|{1, 40}|{2,-10}",
But the problem is related to the alignment. Data comes like in
following format:

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
I want Data comes in following proper alignment format.

01Code 01Desc 2000
12345678901234567
Strange, why do you use {0,10} if it should be {0,17} ?
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
So anybody knows the solutions of this problem, please and please
reply imediately, its urgent.
Use the format with proper number of places or use

String.Format("{0,10}{1, 40} {2,-10}", _
Strings.Left(DS.Table(0).Row(i)("Code").ToString() ,10), _
Strings.Left(DS.Table(0).Row(i)("Description").ToS tring(),40), _
DS.Table(0).Row(i)("Amount").ToString())

requires Imports Microsoft.VisualBasic

--
Greetings
Matthias
Jun 4 '07 #2

"Rahul" <ve**********@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
Friends,

My Problem is related to writing a text file from a database
using .Net. in a particular string format.
I have a dataset with a table, let suppose in this table there are
three fields.
- Code
- Description
- Amount

At the time of writing a text file I use in a for loop -

String.Format("{0,10}{1, 40} {2,-10}", DS.Table(0).Row(i)
("Code").ToString(), DS.Table(0).Row(i)("Description").ToString(),
DS.Table(0).Row(i)("Amount").ToString())
But the problem is related to the alignment. Data comes like in
following format:

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
I want Data comes in following proper alignment format.

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
So anybody knows the solutions of this problem, please and please
reply imediately, its urgent.
out of the many ways to do this, you may want to look at the padLeft and
padRight methods of the String object.
These methods can be used to left-align within a column - or right-align in
a column. It adds (pads) spaces to the left or to the right of the content -
you decide how many.
I think these methods were made for programmers to generate reports.
Jun 4 '07 #3
Rahul,

One thing you could try is to pad each field using the String object's
PadLeft and PadRight methods, such as:

String.Format("{0,10}{1, 40} {2,-10}",
DS.Table(0).Row(i)("Code").ToString().PadRight(15, " "c),
DS.Table(0).Row(i)("Description").ToString().PadRi ght(15, " "c),
DS.Table(0).Row(i)("Amount").ToString().PadRight(1 5, " "c))

The downside to this is that you have to know ahead of time what the maximum
length is for each field.
Hope this helps,

Steve
"Rahul" <ve**********@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
Friends,

My Problem is related to writing a text file from a database
using .Net. in a particular string format.
I have a dataset with a table, let suppose in this table there are
three fields.
- Code
- Description
- Amount

At the time of writing a text file I use in a for loop -

String.Format("{0,10}{1, 40} {2,-10}", DS.Table(0).Row(i)
("Code").ToString(), DS.Table(0).Row(i)("Description").ToString(),
DS.Table(0).Row(i)("Amount").ToString())
But the problem is related to the alignment. Data comes like in
following format:

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
I want Data comes in following proper alignment format.

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000
So anybody knows the solutions of this problem, please and please
reply imediately, its urgent.

Rahul

Jun 5 '07 #4
PlatinumBay wrote:
....
String.Format("{0,10}{1, 40} {2,-10}",
DS.Table(0).Row(i)("Code").ToString().PadRight(15, " "c),
DS.Table(0).Row(i)("Description").ToString().PadRi ght(15, " "c),
DS.Table(0).Row(i)("Amount").ToString().PadRight(1 5, " "c))

The downside to this is that you have to know ahead of time what the maximum
length is for each field.
Hi Steve,
Public Function PadRight(ByVal totalWidth As Integer) As String
Member of System.String

uses the spcae by default.

The c in _" "c")_ in your above code seems to be a typo?
--
Greetings
Matthias
Jun 5 '07 #5
Matthias Tacke wrote:
PlatinumBay wrote:
...
>DS.Table(0).Row(i)("Code").ToString().PadRight(15 , " "c),
The c in _" "c")_ in your above code seems to be a typo?
It tells the computer that it's a single character rather than a string of
one character.

Andrew
Jun 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andy Flash | last post by:
Hi. I'd like a little help working out what is going on with what should be a simple alignment problem. I have an outer table with a <td> which has an inner table. The <td> specified as...
2
by: derek.google | last post by:
I have an application that's crashing because of an alignment problem, and this is the smallest program that demonstrates what's happening: int main() { struct Message { unsigned short size;...
1
by: sgraber | last post by:
I have encountered a class data alignment issue in Microsoft Visual Studio .Net 2003 (C++, 7.1). The issue only appears in certain configurations. My class is a simple class with no inheritance....
2
by: John Smith | last post by:
Hi all; Putting "Due" into the column header of a datagrid. Font is a proportional fort. When the alignment is left, there is some space between the column separator bar and the D in Due....
5
by: jmcpa | last post by:
I have put together a web page for a group and I am having an alignment problem. The page is at: I have a top banner divided into two parts - a top piece and a small clice at the bottom for...
4
by: aamirghanchi | last post by:
Hi, I am hoping if there is a kludge in IE6 to make the textbox align with the top label. It aligns perfectly fine in Opera and in Netscape/Firefox to some extent. Thanks. <style>...
1
by: shalini jain | last post by:
Hi, I am being faced with a strange problem... I wrote a code for displaying pages in HTML and hence was using HTML parser. Now i am using the same code but now parsing using XHTML that is i want...
10
by: Rahul | last post by:
Friends, My Problem is related to writing a text file from a database using .Net. in a particular string format. I have a dataset with a table, let suppose in this table there are three fields....
1
by: David C | last post by:
I have an asp.net page that is adding a javascript onclick event by adding an attribute (see below). The problem is that when the file name (shown as strNewFile) has a single quote in the name...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.