Solved

Strings

Posted on 1998-12-12
5
143 Views
Last Modified: 2012-05-04
This should be a very easy one...  I feel stupid asking....  All I want to do is take a string and be able to extract each word from the string seperately.  In other words if I had a string that said "Jack and Jill went up the hill."  I would want to be able to take each of the words and look at them seperately and maybe do something to each one as I am going through (display them one at a time for instance..  or put them in another control)
0
Comment
Question by:fatalerror080298
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
vikiing earned 50 total points
ID: 1449753
I%=0
Do
   I%=Instr(I%+1, Phrase$, " ")    'Look for a space
   If I%=0 then Exit Do            'If no more spaces, bye
   ThisWord$=Left$(Phrase$, I%-1)  'Pick up a word
   Phrase$=Mid$(Phrase$,I%+1)      'Remove word from phrase
   <do something with ThisWord$>
Loop                               'Keep on going

0
 
LVL 3

Expert Comment

by:vikiing
ID: 1449754
Ops !!!, I forgot to state:

I%=0 : Phrase$=Phrase$+" "   'Add a blank to not loose the last word
0
 

Author Comment

by:fatalerror080298
ID: 1449755
Not sure if this is what you mean in your comment but when I tried the code I noticed that each time I was picking up an extra word because the counter was not being re-initialized in the loop.  Anyway, after adding that it pretty much works.  I didn't really want to erase the originating text though.  But at least you pointed me in the correct direction.

I saw the functions StrSpan and StrBreak that seems like they would do what I want but I just need to figure out the correct usage for this situation.  I can't find any real good examples.

Thanks for your help!
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1449756
Keep in mind that variable I% is NOT a counter, but a pointer indicator of where is the next space into the phrase. When no more spaces are found, I% returns (to) zero. Anyway, that's why just before DO loop I stated  I%=0

Thanks for the points !!
0
 

Author Comment

by:fatalerror080298
ID: 1449757
Found a better way to do it....  I believe this function (split) came around in VB 6...  Very nice...

    Dim i           As Integer
    Dim stringArray As Variant
       
    stringArray = Split(txtString, " ")
   
    For i = LBound(stringArray) To UBound(stringArray)
        msgReturn = MsgBox(stringArray(i), vbOKOnly, "Text Display")
    Next i
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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 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