Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2005, 07:51 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing length of string automatically

Hi Guys

I have to have a string a certain length, for example 10 characters.

So if a user submits a string such as "123456" I need to have a script that automatically inserts a character such as "$" to make up the difference in string length.

so my final output needs to be "123456$$$$"

each user will submit different length string so this needs to be done on the fly.

Any help would be appreciated.


Thanks
 
Old September 3rd, 2005, 10:05 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

dim uString
uString = "123456"
someName(uString,10)

function someName(str,paddTo)
   dim uStringLen
   uStirngLen = len(str)
   do until uStingLen = paddTo
      someName = str = str & "$"
      uStingLen = uStingLen + 1
   loop
end function

Wind is your friend
Matt
 
Old September 4th, 2005, 03:57 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI matt

Thanks a stack. However with your code I'm getting a

"Cannot use parentheses when calling a Sub
testPage.asp, line 4, column 20
someName(uString,10)"

I tried this
"result = someName(uString,10)
response.write result"

but get a value of "False"

thanks
Ryan
 
Old September 4th, 2005, 04:05 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok i thought perhaps it was because a few variables were spelt incorrectly like "uStingLen" instead of "uStringLen" but still get original error.


 
Old September 4th, 2005, 04:20 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok there was another error in the script *** someName = str = str & "$" ***

anyway, I played around till it worked, however I do not understand why it worked (Beginner). here is the code below. My question is after that.

<%option Explicit%>
<%
dim uString, result, uStringLen
uString = "123456"


function someName(str,paddTo)
   dim uStringLen
   uStringLen = len(str)
   do until uStringLen = paddTo
      str = str & "$"
      uStringLen = uStringLen + 1
   loop
end function

result=someName(uString,10)
response.write uString
%>

if I have the line "someName(uString,10)" I get the parentheses error.
however if put in the following "result=someName(uString,10)" and then response.write uString it works.

If I then response.write result I get no value however if I response.write uString to get the result.

What am i missing?


 
Old September 4th, 2005, 06:20 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

I didnt test it when i wrote the function, here is how it should be:

function someName(str,paddTo)
   dim uStringLen
   uStringLen = len(str)
   someName = str
   do until uStringLen = paddTo
      someName = someName & "$"
      uStringLen = uStringLen + 1
  loop
end function

dim uString
uString = "123456"
response.write someName(uString,10)

I find it good practice to have the result held in the function name then you can use it on one line like so:

sql = INSERT INTO tble (fieldName)VALUES('" & someName(uString,10) & "');"

The extra parameter in the function makes it more useable. This way you can change the length of your string when you call the function without having to alter the function. Sorry bout the error in my first post.

Wind is your friend
Matt




Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fix string length Ivanchan Excel VBA 2 July 26th, 2007 08:55 AM
string-length problem alapati.sasi XSLT 3 July 4th, 2007 10:08 AM
How to loop a command for a string length? LordJaffa Excel VBA 5 June 5th, 2007 10:20 PM
Chap 3, "Length of String" richajos BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 February 25th, 2006 08:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.