Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string end

Status
Not open for further replies.

cjkenworthy

Programmer
Sep 13, 2002
237
GB
I've got a string:

(value1, value2, value3, value4,)

I want to trim the string at the end by removing the last comma. Which vbscript function would let me do this - to achieve:


(value1, value2, value3, value4)


 
I could not find a simple utility but this will achieve the result required.

orgstring = "(value1, value2, value3, value4,)"
arrstring = Split(orgstring,",")
for i = 0 to ubound(arrstring) -1
If newstring = "" Then
newstring = arrstring(i)
Else
newstring = newstring & "," & arrstring(i)
End If
Next
newstring = newstring & ")"
msgbox newstring
Regards
Steve Friday
 
The reaseon why I ask this is because I can't get the below code to enter the first 'IF':

(the code reads in the values of a dynamic number of text fields from a form into a sting strTERMNO)

dim formcounter, J

formcounter = Request.Form("hdnrecords")
response.write("Number of fields: " & formcounter) 'WORKS OK
response.write(&quot;<BR>&quot;)

for J=1 to formcounter

response.write(&quot;Counter J is: &quot; & J )
response.write(&quot;<BR>&quot;)

if J = formcounter then 'DOES NOT ENTER HERE
strTERMNO = strTERMNO & Request.Form(&quot;txttermno&quot; & J)
else
strTERMNO = strTERMNO & Request.Form(&quot;txttermno&quot; & J) & &quot;, &quot;
end if

next

I basically want it to use the 'else' for every time it reads txttermno , until the last one (where J = number of fields) e.g if it was teh second iteration and number ofr txttermno's was 2 then it would add it to the string, but not append another comma. Unfortunately it just uses the else every time, and a comma is always at the end.

?
 
Changed your code a bit, it appears not to like the way the number is formated, by checking that j = Abs(formcounter) it seems to work. If you just change the line:

if J = Abs(formcounter) then

Hope it helps


dim formcounter, J

formcounter = &quot;3&quot;
msgbox &quot;Number of fields: &quot; & formcounter 'WORKS OK

For J= 1 to formcounter

msgbox &quot;Counter J is: &quot; & J

msgbox formcounter


if J = ABS(formcounter) then 'DOES NOT ENTER HERE
msgbox &quot;If&quot;
strTERMNO = strTERMNO & &quot;txttermno&quot; & J
else
msgbox &quot;else&quot;
strTERMNO = strTERMNO & &quot;txttermno&quot; & J & &quot;, &quot;
end if

next
msgbox strTERMNO Regards
Steve Friday
 
Just one more note,

if you run Msgbox VarType(formcounter) you will probably get a return of 8 which means it is a string, you could convert this to an integer by doing CInt(formcounter) in which case your If statement could be

if J = CInt(formcounter) then


Regards
Steve Friday
 
Turn it around.

If you put the &quot;, &quot; in front of each concatenated substring, you can use Mid(strTERMNO, 3) to easily chop off the leading &quot;, &quot; after you are done.

dim formcounter, J

formcounter = CInt(Request.Form(&quot;hdnrecords&quot;))

for J=1 to formcounter
strTERMNO = strTERMNO & &quot;, &quot; & Request.Form(&quot;txttermno&quot; & J)
next

response.write(Mid(strTERMNO, 3))
 
I used the CInt option, thanks for the help.

Can for loop conditions only deal with INT's in the conditions?


Thanks again
 
Not neccessarily, you could do a loop

Do until testvar = &quot;Found&quot;
blah blah blah
Loop

but it does appear that you should always check that your variable is an integer and not a string when performing a numerical loop check. I was not aware of this issue until I started playing with your code.

We all learn something new every day.





Regards
Steve Friday
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top