chisholmd
asked on
vbscript replace
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
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
oops... Last line before the loop kindly replace with this line
tmpStr = tmpStr + Text_get & "<br>"
Hope this help...
tmpStr = tmpStr + Text_get & "<br>"
Hope this help...
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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! :)
==========================
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...