Home > ASP Development > Parsing a string problem

Parsing a string problem



Hello



I have a little bugging problem and need some help on this please.



If I had a string that look like this:



JOHNDOE|12,45,67,89|

EMILY|12,34,56|

SAWYER||



How do I parse the string in getting the names out first i.e JOHNDOE?



How do I then get the numbers out separately if they do exists? i.e. 12

45



I am thinking it is in a loop with instr or split function. I can see how I can get the numbers out first but with | and , at the same time - not sure.



An example would be most helpful.



Appreciate anyone out there who could offer some help please.



Thanks

    
Guest


hi,

you can try somthing like this


Code:



theString = "JOHNDOE|12,45,67,89|"

'split into seperate items
stringArr = split(theString, "|")

'get the name
theName = strArr(0)
response.write "Name: " & theName

'get all the numbers
allNumbers = strArr(1)

'check to see if it contains more than 1 number
if inStr(allNumbers,",") > 0 then
allNoArr = split(allNumbers, ",")
response.write "Numbers<br/>"
for i = 0 to ubound(allNoArr)
response.write "Number " & (i + 1) & " : " & allNoArr(i) & "<br/>"
next
else
response.write "Only One Number: " & allNumbers
end if




hope this helps

Was this answer helpful ? Yes No   
Guest


Thank you for your help, my code was nearly there in what you had in mind or your actual code.



I just realised something; I tried using this string in a similar format:



JOHNDOE|A,42,1,4,5|B,6,7,4,2|C,2,4,6,7|





Now in accordance with your code, should there be another If instr to look for | before calling the If statement to check for , (comma).



So it looks like this:



JOHNDOE



A

42

1

5



a break line



B

6

7

4

2



I just want to see if I am thinking on the right track about putting another If statement for |.

Not sure though.

Even though it does sound simple.



So any help would be appreciated please.



Thanks again

Was this answer helpful ? Yes No   
Guest


nofriends code with work for the initial format you provided, since you've added additional "|" to the string his code would need to be modified to look at the additional subscripts of the array after splitting the initial string.

Was this answer helpful ? Yes No   
Guest



Code:




<%
Function parseString(sString, sDelimiter)
iNamePos = INSTR(sString, "|")
sName = LEFT(sString, iNamePos - 1)
sString = REPLACE(MID(sString, iNamePos + 1), "|", sDelimiter)
aString = SPLIT(sString, ",")
iLower = LBOUND(aString)
iUpper = UBOUND(aString)

Response.Write UCASE(sName) & "<p>"

For i = iLower To iUpper
Response.Write aString(i) & "<br>"
Next
End Function

Call parseString("JOHNDOE|A,42,1,4,5|B,6,7,4,2|C,2,4,6,7|", "<hr>")
%>



Was this answer helpful ? Yes No   
Guest


you should use Split, not InStr.

and when reading the array items, you have to first verify they exist:


Code:



'get the name
theName = ""
If UBound(strArr)>-1 Then theName = strArr(0)
response.write "Name: " & theName

'get all the numbers
allNumbers = ""
If UBound(strArr)>0 Then allNumbers = strArr(1)

...



Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by Shadow Wizard
you should use Split, not InStr.

and when reading the array items, you have to first verify they exist:


Code:



'get the name
theName = ""
If UBound(strArr)>-1 Then theName = strArr(0)
response.write "Name: " & theName

'get all the numbers
allNumbers = ""
If UBound(strArr)>0 Then allNumbers = strArr(1)

...










would this work...




Code:



If UBound(strArr)>0 Then allNumbers = strArr(1)






on...



JOHNDOE|A,42,1,4,5|B,6,7,4,2|C,2,4,6,7|



...not ok to use instr and split?...(i saw your shouldn't but no explanation so please explain why?)...and would isArray be better than 2 if conditions?



just curious ...learning ..thanks

Was this answer helpful ? Yes No   
Guest


yes it would work, as the UBound of the array would be 4, and the second

item will be "A,42,1,4,5". so, the variable allNumbers will be given the value

"A,42,1,4,5" as requested. (as far as I understood what has been asked)

InStr is not relevant, as in case the "|" is not found, you will get array with

one item only and UBound of 0 and this is exactly what I check.

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2008 by Infoqu. All rights reserved