Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i tokenize a string? 1

Status
Not open for further replies.

cpu90

Programmer
Joined
Aug 8, 2003
Messages
2
Location
US
hi guys, i have this ergent problem: i read in a line from a file, and i want only part of it, how do i split the line that i just read?
so here is the code:

buf = file.ReadLine

lets say the line is "Hello everybody here", so now buf is that line. but how do i get "Hello" or "everybody" from buf ? i can't just go for buf[1] as its index, i'm lost, sombody plz help, i need it asap
 
There are at least two ways to do this. I will present the brute force method:


Code:
strString = "Hello everyone out there."

strLen = Len(strString)
' iterate through string
For nCount = 1 To strLen - 1
   ' find position of character 
   nSpacePos = Instr(strString, " ")
   If nSpacePos > 0 Then
      strToken = Left(strString, nSpacePos - 1)
      nStrLen = Len(strString)
      ' truncate string to remove token
      strString = Right(strString, nStrLen - nSpacePos)
   End If
Next


However, I think a better way would be to use the RegExp object. Do a keyword search here for RegExp. There is an excellent post by clarkin under the topic "Regular Expression Help - Almost got it..." that describes how to use the RegExp object.

Zy
 
You can simplify the brute force solution by using the Split function, eg

buf=Split(file.readline," ")

 
Split() and Replace() are the VBscript functions you probably want to check out for your string manipulation (see Zymurgyst's link).

But it's quite likely Regular Expressions can do everything you want & more - they just take a little sideways push to get your head around their slightly different approach (as compared to tokenizing and working from there).

This page is very comprehensive:

I've helped out a few peeps with Reg Exps in this forum - let us know what you are trying to achieve with your tokenized string? It may be Reg Exps are better than tokenizing. " i want only part of it " - probably means they will be ideal :)


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top