[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

How to format string output?

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
joel011197
Asked:
joel011197
1 Solution
 
thunderchickenCommented:
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
 
joel011197Author Commented:
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
 
makerpCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
makerpCommented:
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
 
makerpCommented:
whops i got thje params wrong way round in the string function

String(width,string)

String((Col1_width - Len(my_field))," ")
0
 
joel011197Author Commented:
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
 
rupertsCommented:
So if it is an 8 character string do you want to follow it with a character like '*' ?

0
 
joel011197Author Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now