Solved

How to format string output?

Posted on 2000-04-06
8
352 Views
Last Modified: 2006-11-17
I need to generate a report (text on several columns) using ASP pages. Data are extracted from a database and of course they don't have always ther same length.  Using other langages like C or Perl, there is a way to define how many characters are available to display a string (C is printf %15s, indicate that the string has always 15 position to be displayed). Of course a font like Courier will be used.

Is there something equivalent using ASP/VBScript?

0
Comment
Question by:joel011197
8 Comments
 
LVL 11

Expert Comment

by:thunderchicken
Comment Utility
i don't know if this is what your looking for, but you can get certain text from a string using the MID function

<%
dim thetext
thetext = MID(yourstring,<start position>,<total length>)
response.write(thetext)
%>

this would display the first 15 characters if you put

thetext = MID(yourstring,1,15)
0
 

Author Comment

by:joel011197
Comment Utility
Ok with the Mid function, but if the string is not 15 character long but less (let's say 8), it will only use 8 positions and the display the follwoing information. I would expect to make a kind a tabulator using a format function.
0
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
Comment Utility
define your colunm widths as variables

String(string,number) returns a string number long made of string

then

String(" ",(Col_width - Len(your_string)))

so each time you print a field do this

define each col width i.e.

col1_width = 30

obj.WriteLine(my_field&String(" ",(Col1_width - Len(my_field))))
 
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
Function Format(string,width)
    Format = string&String(" ",(width - Len(string)))
End Function

then simply wrap output in that

obj.writeLine(Format(my_field,col1_width))
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:makerp
Comment Utility
whops i got thje params wrong way round in the string function

String(width,string)

String((Col1_width - Len(my_field))," ")
0
 

Author Comment

by:joel011197
Comment Utility
Ok with the Mid function, but if the string is not 15 character long but less (let's say 8), it will only use 8 positions and the display the follwoing information. I would expect to make a kind a tabulator using a format function.
0
 
LVL 4

Expert Comment

by:ruperts
Comment Utility
So if it is an 8 character string do you want to follow it with a character like '*' ?

0
 

Author Comment

by:joel011197
Comment Utility
I did not know the String function, so I can fill my string with the desired character and so execute a knid of format operation. Thanks for your suggestions.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now