vbscript replace

chisholmd
chisholmd used Ask the Experts™
on
I am having troubles with the replace syntax in vbscript.

MY GOAL:
I need to build a custom word wrap for passing a string to a componant.  The string will be the title on a chart and I want the title to wrap at approx 40 characters.

VBSCRIPT REPLACE SYNTAX
Replace(expression, find, replacewith[, start[, count[, compare]]])

MY CODE:
Function titlewrap(vData)
     TitleLen = len(vData)
     tmpStr = vData
     x = 40
     do while titlelen > x
             tmpStr = Replace(tmpStr," ","<br>",x,1,1)
          x = x + 40
     loop
     titlewrap = tmpStr
End Function

INPUT:

The feeling of security and the lighting in the parking lot

EXPECTED OUTPUT:
The feeling of security and the lighting<br>
in the parking lot

ACTUAL OUTPUT:
g
in the parking lot


My expectation was that by setting the start value at say 40 and the count (number of instances to replace) to 1 that I would replace the first space after the 40th character with a "<br>". ON the next pass through the loop it would start at 80 then 120 etc if the string was that long.

Confused and hoping I am blind to the obvious.
Dave Chisholm
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can try this one though but using if statement... You can change the Allow_FieldSize..

===============================

Text_storage = "The feeling of security and the lighting in the parking lot"

tcounter = 0

Do While Not (Len(Text_storage) = 0)

Allow_FieldSize = 40

tmp_Text = Text_storage

tcounter = Text_count

Text_Len = Len(tmp_Text)

Text_get = ""

    For y = 1 To Text_Len

                'check for space position
               
                If Mid(tmp_Text, y, 1) = " " Then
           
                    space_count = y
       
                End If
       
            'check if text is already greater than the allowable field size
       
                If Len(Text_get) < Allow_FieldSize + 1 Then
                       
                    Text_get = Text_get & Mid(tmp_Text, y, 1)
       
                    'check if the temporary text length is equal to the lenght text to be save
                    If Len(Text_get) = Text_Len Then
       
                        tmp_Text = ""
       
                        tcounter = tcounter + 1
       
                        y = Text_Len
       
                    End If
       
                Else
           
                    If Not (Mid(tmp_Text, y, 1) = " ") Then
           
                        If space_count <> 0 Then
                       
                            Text_get = Left(tmp_Text, space_count)
           
                            tmp_Text = Mid(tmp_Text, space_count + 1, Text_Len)
           
                            tcounter = tcounter + 1
                         
                            y = Text_Len
           
                        Else
           
           
                            Text_get = Left(tmp_Text, Allow_FieldSize)
           
                            tmp_Text = Mid(tmp_Text, Allow_FieldSize + 1, Text_Len)
           
                            tcounter = tcounter + 1
                         
                            y = Text_Len
           
                        End If
           
                End If
           
        End If
       
    Next

     
Text_count = tcounter
 
Text_storage = tmp_Text

tmpStr = Text_get & "<br>"

Loop

response.write tmpstr


=======================================

Hope this help...

Happy programming...






Commented:
oops... Last line before the loop kindly replace with this line

tmpStr = tmpStr + Text_get & "<br>"

Hope this help...



Author

Commented:
Thanks gladxml your code works fine of course.  Not to sound ungratefull but I was trying to avoid having to use something so verbose.  (no reflection on the quality of your code)  And being the lazy programmer that I amI will use it.

The larger mystery remains for me as to why the replace function did not work as intended.  Is all the documentation wrong?  Im I reading it wrong.  I am still confused as to how the function and my code generated the results it did.

I will accept you comments but if you don't mind I will leave this open for a day to see if anyone can still answer what was wrong with my code so that I can understand.

Thanks,
Dave

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Commented:
Function titlewrap(vData)
    titlelen = Len(vData)
    tmpStr = vData
    x = 40
    Do While titlelen > x
        tmpStr = Left(tmpStr, x - 1) & Replace(tmpStr, " ", "<br>", x, 1, 1)
         x = x + 40
    Loop
    titlewrap = tmpStr
End Function


this is from MSDN, Note the last sentence:
The return value of the Replace function is a string, with substitutions made, that begins at the position specified by start and and concludes at the end of the expression string. It is not a copy of the original string from start to finish.

Chis,
    Heya. Well, it's because you're beginning @ character point 40 instead of 1. Replace won't work anyway.. because you need to jump from point 40, to point 80, to point 120 and then make sure that each of those points is a space. To get the first 40 characters that you skipped, you'd use:

Function titlewrap(vData)
    TitleLen = len(vData)
    tmpStr = vData
    x = 1
    do while titlelen > x
            tmpStr = Replace(tmpStr," ","<br>",x,1,1)
         x = x + 40
    loop
    titlewrap = tmpStr
End Function

But that won't work for this. If anything, you could do something like this:

<%= titleWrap("My expectation was that by setting the start value at say 40 and the count (number of instances to replace) to 1 that I would replace the first space after the 40th character with a br. ON the next pass through the loop it would start at 80 then 120 etc if the string was that long.", 40) %>

<%
Function titlewrap(vData, vLength)
    TitleLen = len(vData)
    tmpStart = 1
    CharCount = 1
    for i = 1 to TitleLen
     If tmpStart > vLength Then
          Char = Mid(vData, CharCount, 1)
          If Char = " " Then
               tmpStr = tmpStr & " <br>"
               tmpStart = 1
          Else
               tmpStr = tmpStr & Mid(vData, CharCount, 1)
               tmpStart = tmpStart + 1
          End If
     Else
          tmpStr = tmpStr & Mid(vData, CharCount, 1)
          tmpStart = tmpStart + 1
     End If
     CharCount = CharCount + 1
    next
    titlewrap = tmpStr
End Function
%>

Hope this helps!

-Matt
Whoops, answer accepted. Cool. Enjoy anyway! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial