famtrad
asked on
Extracting a string that is separated by commas
Hi,
I have this string that contain items, and each item is separated by a comma.
For example:
software, hardware, mouse, keyboard, etc.
What I want to do is to extract each of those words out and assign it to another variable, so that the new variable will end up storing: software,hardware,mouse,ke yboard,etc .
I know I'm suppose to use the instr() and mid() but I'm stuck on the looping part, like going through the whole string and extracting it.
My main purpose for this is to put that new variable that stores the string without the space after the commas into a hidden text box, so mainly I'm extracting the words from the string is to take out the spaces. Am I going through the right approach or there's another way of doing this?
Thank you in advance.
I have this string that contain items, and each item is separated by a comma.
For example:
software, hardware, mouse, keyboard, etc.
What I want to do is to extract each of those words out and assign it to another variable, so that the new variable will end up storing: software,hardware,mouse,ke
I know I'm suppose to use the instr() and mid() but I'm stuck on the looping part, like going through the whole string and extracting it.
My main purpose for this is to put that new variable that stores the string without the space after the commas into a hidden text box, so mainly I'm extracting the words from the string is to take out the spaces. Am I going through the right approach or there's another way of doing this?
Thank you in advance.
Hi famtrad,
You can split a string into an array:
dim MyArray() as string ' dynamic array, don't specify the no of elements
MyArray = split(liststring, ",") ' that in quotes is the list separator
then you can loop through the array to handle each element
dim i as integer
for i = 0 to Ubound(MyArray) - 1 ' by default array bounds start at 0
DoSomethingWith MyArray(i)
next
Regards .. Alan
You can split a string into an array:
dim MyArray() as string ' dynamic array, don't specify the no of elements
MyArray = split(liststring, ",") ' that in quotes is the list separator
then you can loop through the array to handle each element
dim i as integer
for i = 0 to Ubound(MyArray) - 1 ' by default array bounds start at 0
DoSomethingWith MyArray(i)
next
Regards .. Alan
I was about to write the loop using instr() ...
but R_Rajesh has sure refreshed my memory about a function that I have not used for a long time.
Thanks R_Rajesh - that was cool.
Gajendra
but R_Rajesh has sure refreshed my memory about a function that I have not used for a long time.
Thanks R_Rajesh - that was cool.
Gajendra
famtrad,
if you are simply removing the spaces then try this
aa = "software, hardware, mouse, keyboard"
t = Split(aa, ",")
For z = 0 To UBound(t)
newaa = newaa & Trim(t(z)) & IIf(z <> UBound(t), ",", "")
Next z
MsgBox newaa
Rajesh
if you are simply removing the spaces then try this
aa = "software, hardware, mouse, keyboard"
t = Split(aa, ",")
For z = 0 To UBound(t)
newaa = newaa & Trim(t(z)) & IIf(z <> UBound(t), ",", "")
Next z
MsgBox newaa
Rajesh
ASKER
Hi,
I'm assuming I can use these codes as vbscript since they are pretty similar.
Raj:
Since you putted it in a msgbox, I can convert that msgbox into a variable right, so can I do it like this?
---
aa = "software, hardware, mouse, keyboard"
t = Split(aa, ",")
For z = 0 To UBound(t)
item = t(z)
Next z
---
and then put it in a hidden text like this?
---
<input type="hidden" value="<%=item%>">
---
will that work? That will give me all the items correct? Not just one right?
I'm assuming I can use these codes as vbscript since they are pretty similar.
Raj:
Since you putted it in a msgbox, I can convert that msgbox into a variable right, so can I do it like this?
---
aa = "software, hardware, mouse, keyboard"
t = Split(aa, ",")
For z = 0 To UBound(t)
item = t(z)
Next z
---
and then put it in a hidden text like this?
---
<input type="hidden" value="<%=item%>">
---
will that work? That will give me all the items correct? Not just one right?
aa = "software, hardware, mouse, keyboard"
t = Split(aa, ",")
For z = 0 To UBound(t)
newaa = newaa & Trim(t(z)) & IIf(z <> UBound(t), ",", "")
Next z
here the variable newaa will contain all the items munus the spaces
t = Split(aa, ",")
For z = 0 To UBound(t)
newaa = newaa & Trim(t(z)) & IIf(z <> UBound(t), ",", "")
Next z
here the variable newaa will contain all the items munus the spaces
To remove the spaces,just use Replace:
aa = "software, hardware, mouse, keyboard"
aa = Replace(aa," ","")
aa = "software, hardware, mouse, keyboard"
aa = Replace(aa," ","")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for helping me out.
Raj:
I've tried your way but it always gives me syntax error and I've tried debugging it but still does not work but thanks for helping though.
I've tried other ways that were posted but only aelatik solution works so I'm giving my points to him. Thanks Aelatik.
Also thank you all for helping.
Raj:
I've tried your way but it always gives me syntax error and I've tried debugging it but still does not work but thanks for helping though.
I've tried other ways that were posted but only aelatik solution works so I'm giving my points to him. Thanks Aelatik.
Also thank you all for helping.
use the split function
aa = "software, hardware, mouse, keyboard"
t = Split(aa, ",")
For z = 0 To UBound(t)
MsgBox t(z)
Next z
Cheers!
Raj