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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse out a string

Status
Not open for further replies.

huobaji

Technical User
Sep 20, 2010
23
US
Can anyone help me with this?
I'm trying to parse out this String into variables. I'm assuming I would have to loop through the string.

Here is the string but this string would always be different.
So every time there is a "-" strip the data out and when there is a "," it would loop and strip those out

example String

Example130-Your Analysis Bus-0.00-W-20032,Help130-My Analysis Aug-0.33-C-20052

example results
Example130
Your Analysis Bus
0.00
W
20032

loop again

example results
Help130
My Analysis Aug
0.33
C
20052


 
Code:
<%
c = "Example130-Your Analysis Bus-0.00-W-20032,Help130-My Analysis Aug-0.33-C-20052"

a = split(c,"-")
for i = 0 to ubound(a)
 response.Write a(i) & "<br>"
next
%>
 
sorry, now i see the , in that string.
so there is an outer loop for that code, in which you do a left( string, "comma position")

you find that (1st) "comma position" by using the InStr function.
 
When I run the code I get this
Response object error 'ASP 0106 : 80020005'

Type Mismatch

An unhandled data type was encountered.
 
superstrange, it works on my box.
i modified it a little to show you the string manipulation:
Code:
<%

cTheString = "Example130-Your Analysis Bus-0.00-W-20032,Help130-My Analysis Aug-0.33-C-20052"
response.Write cTheString
 
do while cTheString <> ""
 'Where is the first comma?
 
 nComma = instr(cTheString, ",")
 if nComma = 0 then
  cSub = cTheString
  cTheString = ""   ' will exit the loop
 else
  cSub = left(cTheString, nComma-1)
  cTheString = mid(cTheString, nComma+1)  
 end if
 response.Write "<hr>"
 a = split(cSub,"-")
 for i = 0 to ubound(a)
  response.Write a(i) & "<br>"
 next

loop
%>
 
you could do a replace and split all by -...however, splitting may be a moot point if you cannot guarantee 'needed' dashes or commas in the string...therefore a regular expression may help

Code:
<%
  s = Replace("Example130-Your Analysis Bus-0.00-W-20032,Help130-My Analysis Aug-0.33-C-20052",",","-")
  a = Split(s,"-")

  For i = 0 to UBound(a)

   Execute("Var" & i & "=a(i)")
   Response.Write a(i) & "<br>"
  Next

  response.write Var1 & " " & Var2 ' ...
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top