...

View Full Version : Splitting strings in VBscript?



Spudhead
07-30-2002, 12:30 PM
Hello,

I'm having a bit of bother with the few lines of VBscript below - I usually do ASP in Jscript and I'm not sure what's up with it?


splitDBF=Split(Cstr(rs.Fields("database_field").value), "+")
dbTable=splitDBF(0)
dbColumn=splitDBF(1)

This is in a chunk of code that loops through a recordset: the 'rs.Fields("database_field").value' bit definitely supplies a string that looks (for eg.) like "employees+first_name".

I get an error on the 'dbColumn=splitDBF(1)' line, saying:


Subscript out of range: '[number: 1]'


What am I doing wrong?

Cheers.

allida77
07-30-2002, 01:43 PM
Try doing a

Response.Write(UBound(splitDBF))
Response.Write(Ubound(dbTable))
Response.Write(Ubound(dbColumn))

Before each split to see how many elements are being put into the array.

It looks like on dbTable you are splitting by " ", then on dbColumn you are again splitting using " ". Do you want to split by the same delimiter twice?

Spudhead
07-31-2002, 10:20 AM
Pardon? Thanks for your help, I think I may have got myself a little lost somewhere.

I've got a string that looks like "tableName+columnName" coming out of a database recordset.

All I want to do is split it on the "+".

So I've set the variable "splitDBF" to be an Array - which is returned from running the Split() function on that recordset value, and which holds what I assume are only 2 elements:

dbTable="tableName" [the first element in the zero-based array "splitDBF"]
dbColumn="columnName" [the second element in the array]

Is that not what the bit of code I posted does? Am I splitting something twice?

Cheers again...

glenngv
07-31-2002, 10:34 AM
there may be some fields which doesnt have + sign. try to verify by running this code:

field = Cstr(rs.Fields("database_field").value)
response.write "Field: " & field & "<br>"
splitDBF=Split(field, "+")
response.write "Ubound: " & ubound(splitDBF) & "<br>"
for i = 0 to ubound(splitDBF)
response.write i & ": " & splitDBF(i) & "<br>"
next

Spudhead
07-31-2002, 11:28 AM
Hmmm - this gets weirder.

There are indeed some records that don't contain the "+" sign. I THOUGHT I was catching that, but it turns out I'm not - and that's probably why it falls over when it gets to one.

So - this InStr() function. Does it actually work? If it doesn't find a match, it returns "0", right? Could anyone explain why the following:


Do While Not rs.EOF

mydbString=CStr(rs.Fields("database_field").value)

Response.Write mydbString & " - "
Response.Write InStr("employees", mydbString)
Response.Write "<br>"

rs.MoveNext
Loop


results in my browser displaying:



employees+name_first - 0
employees+name_last - 0
addresses+email - 0
-- - 0



??

Cheers....

Spudhead
07-31-2002, 11:30 AM
AAAAAAAAAAAHHHHHHHHHHHH!!!

I've just realised.

Sorry everyone.

*Runs and hides sheepishly under his desk before swapping the argumments round for InStr()*


DOH.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum