Solved

How to format string output?

Posted on 2000-04-06
8
354 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
ID: 2690067
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
ID: 2690088
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
ID: 2690107
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 10

Expert Comment

by:makerp
ID: 2690123
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
 
LVL 10

Expert Comment

by:makerp
ID: 2690137
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
ID: 2690143
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
ID: 2693340
So if it is an 8 character string do you want to follow it with a character like '*' ?

0
 

Author Comment

by:joel011197
ID: 2693386
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

12 Experts available now in Live!

Get 1:1 Help Now