Solved

Extracting a string that is separated by commas

Posted on 2003-11-27
9
526 Views
Last Modified: 2010-08-05
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,keyboard,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.

0
Comment
Question by:famtrad
9 Comments
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9831516
Hi famtrad,

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
0
 
LVL 10

Expert Comment

by:ADSaunders
ID: 9831559
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
0
 
LVL 3

Expert Comment

by:gajendra
ID: 9831562
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9831575
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
0
 

Author Comment

by:famtrad
ID: 9831630
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?
       
0
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9831690
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
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9831845
To remove the spaces,just use Replace:

aa = "software, hardware, mouse, keyboard"
aa = Replace(aa," ","")



0
 
LVL 14

Accepted Solution

by:
aelatik earned 125 total points
ID: 9832944
<%
    Dim buffer, item
        buffer = "software,hardware,mouse,keyboard"
    For Each item In Split(buffer, ",")
        Response.write("<input type='hidden' value='" & item & "'>"
    Next
%>
0
 

Author Comment

by:famtrad
ID: 9833051
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.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Acccess - Update Field in Table when report is printed 6 45
VB6 Compile Compatibility Issue 4 102
MS Date Picker 64 bit 32 bit issue 12 54
Passing a Text Box name to a Sub 6 96
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question