Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Active Server Pages (ASP)
Expertise: Beginner
Oct 1, 1996

Finding the End of a Word

Question:
I am new to VB and must learn it by yesterday! I am working with text strings and must figure out how to pull words from a line by an ACSII character that changes. So far I am opening a text file for sequential input and testing for the required character line by line, however, how do I find the beginning and end of the word and send the "word" to output?

Answer:
Well, you can probably check for a piece of punctuation, like a space or something. The InStr function can look in a string for a particular character. If you know for sure that you don't have punctuation and just spaces, it will be very quick.

In order to pull the actual word out of the string you can use the Mid (aka Mid$) function. For instance, let's say you want to pull the "username" out of some text data. You know that it will say "Username:", then a space, then the username itself, followed by another space. We'll say that the string you've read in from the file is already in the variable "WorkString" The code would look something like this:

Dim temp As String
Dim Name As String
Dim Startpoint As Integer

'this line sets the point in the line where you're going to start pulling out the characters of the name
'it looks for the colon and then adds one for the space and one more for the actual first letter of the name
Startpoint = InStr(WorkString, ":") + 2

'now we're going into a Do/Loop
Do
  'pull out the first character here.  this line says "Go to the startpoint in workstring and retrieve one letter
  temp = Mid(WorkString, Startpoint, 1)
    If temp <> " " Then                'This line checks to see if it's a space (i.e. the end of the name)
      Name = Name & temp          'if not, it adds it to the Name string
      Startpoint = Startpoint + 1    'and increments Startpoint by one for the next pass through the loop
    Else
      Exit Do                                 'if it is a space, we exit from the do loop.
    End If
Loop
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date