Solved

Strings

Posted on 1998-12-12
5
142 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

943 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

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now