Wrox Programmer Forums
|
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 March 11th, 2009, 03:33 PM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Red face Adding spaces to a string

Hi,

I need to add spaces to a string. For example:

123456 should become 1 2 3 4 5 6

I've tried split and mergestring commands, but just can't seem to get it quite right. I know this should be simple.

I'm using ASP and some VBscript.

Thanks for any and all suggestions .
 
Old March 11th, 2009, 04:55 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Unfortunately ASP does not have a built in function to do this. The function found here will do (almost) what you need it to do: http://vbcity.com/forums/topic.asp?tid=143771

This will break your string into a sequence of Chracters which then you can iterate through like you would any normal array. You just need to append the spaces to the string as you go.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old March 11th, 2009, 05:04 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

.. ah beat me to it doug, i found the same post

Here is a generic function to split and add any string you want:
VB Code:
Function InjectString(strInput, strInject, strDelimiter)

    ' Special case - passing empty string
    If strInput = "" Then
        InjectString = ""
        Exit Function
    End If
   
    ' Special case - passing empty injection string
    If strInput = "" Then
        InjectString = strInput
        Exit Function
    End If

' split the string up
    If strDelimiter = "" Then
        ' cannot split on empty string

' create an array the same size as the string
        Dim arr()
        iLen = Len(strInput)-1
        ReDim arr(iLen)

' loop string and extract 1 letter at a time
        For intCounter = 0 to iLen
            arr(intCounter) = Mid(strInput, intCounter + 1,1)
        Next
    Else
        ' do a normal split
        arr = Split(strMain, strDelimiter)
    End If

    ' build up final string starting with first element (we always have at least 1)
    strReturn = arr(0)
    ' add other elements with the injection string if there are any
    If UBound(arr) > 0 Then
        For i = 1 To UBound(arr)
            strReturn = strReturn & strInject & arr(i)
        Next
    End If
   
    ' return final string
    InjectString = strReturn

End Function
The trickiest bit is handling the fact that if you split on an empty string, you get an array with just 1 element which has the full string in it - not particularly useful. So we have to build up our own array. Once we have split it into an array, we just loop through it and add the spaces in between each one.

You can call it like so:
vb Code:
spacedString = InjectString("abcdef", " ", "")
Response.Write spacedString

HTH
Phil

Last edited by philip_cole; March 11th, 2009 at 05:08 PM.. Reason: Added code to call it
 
Old March 12th, 2009, 12:38 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Talking

How much do you guys want to kick yourselves??
Code:
<%
input = "ABCDEFG"

Set reIn = New RegExp
reIn.Pattern = "(.)"
reIn.Global = True

Response.Write reIn.Replace( input, "$1 " )
%>
Yes, ASP *does* have a builtin way to do this. Called a regular expression.

It's not really even worth making it into a function, but if you must:
Code:
<%
' you can create the regexp just one time at top of page:
Set reInject = New RegExp
reInject.Pattern = "(.)"
reInject.Global = True

' and then:
Function InjectString( input, injection )
   InjectString = reInject.Replace( input, "$1" & injection )
End Function

' example:
Response.Write "The cow jumped over the moon ==>> " _
     & InjectString("The cow jumped over the moon", "^")
%>
<HR>
<%=InjectString("ABCDEFG","^-^")%>

Last edited by Old Pedant; March 12th, 2009 at 12:40 AM.. Reason: fix typo
 
Old March 23rd, 2009, 11:31 AM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Cool Thanks, but....

Hey all...thanks so much for the great info. However....when I try to use the code posted by OldPedant...I get an error "Can't use parentheses when calling a sub." (It works great until I put it inside my other coding.)

I need to include this in some other code, I'm not really writing this to the screen, but changing a pre-existing variable to a new variable string with spaces.

We're using a screen reader to relay a number but without the spaces it reads it as 9 hundred 20 thousand 3 hundred 15, instead of 9 2 0 3 1 5.

Any help is always appreciated.
 
Old March 23rd, 2009, 11:38 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

@Old Pendant: Obviously by "built in" I meant a predefined function that can be called by the developer without requiring any further coding on their part. ;]

@jroxit: Can you show us the problematic code?

-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old March 23rd, 2009, 11:57 AM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code

Doug,

I wish I could post the code, unfortunately company policy won't allow me to do so.

What I have is a variable, let's say it's named "joesref". Our screen reader needs to read this number back as individual numbers. Instead of reading 982315 as "nine hundred eighty-two thousand three hundred and fifteen" it needs to read it back as "nine eight two three one five". I was told by the screen reader company that I'd need to inject a space between each digit so that it would be read correctly.

It sounded like an easy thing to do...but with Classic ASP and VBscript, maybe not quite so easy after all.

Thanks!
 
Old March 23rd, 2009, 12:05 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

It just tried this, based on old pedant's code and it worked fine:
Code:
<%
joesref = "920315"

Set reIn = New RegExp
reIn.Pattern = "(.)"
reIn.Global = True
spacedString = reIn.Replace(joesref, "$1 " )

Response.Write "Your number is " & spacedString
%>
Output Code:
Your number is 9 2 0 3 1 5

Phil
 
Old March 23rd, 2009, 12:07 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. I am taking a stab in the dark here but when you have a sub routine (e.g. a block of code that does something but doesnt return anything) you would call it like this:

MySub

and with parameters

MySub Parameter1, Parameter2

The error you are recieving is indicative of this type of call to the sub routine:

MySub(Parameter1, Parameter2)

Are you using the code exactly as Old Pendant has provided it?

-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old March 23rd, 2009, 12:14 PM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exactly?

I was before I inserted it into my code...and it worked fine. But once I put it in my previous code, it started giving me that error code.

What if I wanted instead of the "Response.Write" part to set my "joesref" variable equal to the new string?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace the string has comma with spaces? kumiko Classic ASP Basics 3 January 10th, 2008 06:41 PM
adding varchar string to hyperlink dgreep PHP Databases 1 February 19th, 2007 05:56 AM
adding multiple spaces otter606 Javascript 2 March 3rd, 2005 11:00 AM
Adding functionality to the String class? RobMeade BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 5 February 9th, 2005 02:11 PM





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