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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string

Status
Not open for further replies.

tdfreeman

MIS
Mar 28, 2002
85
US
I am reading from a file. I have a line that I need to parse. The line is similar to what follows:

Name 1 [Code1] Name 2 [Code2] Name 3 [Code3]

I need to get the codes from the line and put them into variables. Is there a easy way to parse the line into variables?

Thanks for your help.

Tammy Thank you for your help.

Tammy
 
what type of file is this csv,formated text file, file with no formating, is there 1 record per line? what do you want to do whit infomation?
speak up so we can help you
 
from ---Posted by Dev Ashish---

by CUOK
--------------------------------------------------------------------------------------
Strings: Parsing character separated string into individual components
--------------------------------------------------------------------------------------

(Q) I have string which contains values separated by a comma/colon/semi colon/space. How can I extract each value from that string?

(A) You can use these two functions provided by Microsoft to retrieve each value. Note that the functions are written for comma separated values but can easily be modified to work with any other character. Use the Sub Test as an example

'******************* Code Start ****************
Function CountCSWords(ByVal s) As Integer
'Counts the words in a string that are separated by commas.
Dim WC As Integer, Pos As Integer
If VarType(s) <> 8 Or Len(s) = 0 Then
CountCSWords = 0
Exit Function
End If
WC = 1
Pos = InStr(s, &quot;,&quot;)
Do While Pos > 0
WC = WC + 1
Pos = InStr(Pos + 1, s, &quot;,&quot;)
Loop
CountCSWords = WC
End Function

Function GetCSWord(ByVal s, Indx As Integer)
'Returns the nth word in a specific field.
Dim WC As Integer, Count As Integer
Dim SPos As Integer, EPos As Integer

WC = CountCSWords(s)
If Indx < 1 Or Indx > WC Then
GetCSWord = Null
Exit Function
End If
Count = 1
SPos = 1
For Count = 2 To Indx
SPos = InStr(SPos, s, &quot;,&quot;) + 1
Next Count
EPos = InStr(SPos, s, &quot;,&quot;) - 1
If EPos <= 0 Then EPos = Len(s)
GetCSWord = Trim(Mid(s, SPos, EPos - SPos + 1))
End Function

Sub Test()

Dim strAString As String
Dim I As Integer
Dim intCnt As Integer

strAString = &quot;This,calls,the,two,functions,listed,above&quot;

'Find out how many comma separated words
'are present
intCnt = CountCSWords(strAString)


'Now call the other function to retrieve each one in turn
For I = 1 To intCnt
Debug.Print GetCSWord(strAString, I)
Next
End Sub
'******************* Code End ****************

good luck
CUOK


 
FYI, I found a simple way to parse a string.

Use the function split().

I create an array, split the line into the array and reference the elements of each array.

The following is an example of how I used this function.

Dim arrBudget() As String

arrBudget = Split(Trim(s))
intCount = 0
For Each I In arrBudget
intCount = intCount + 1
Next I 'For Each I In arrBudget
Select Case intCount
Case 1
strFund = arrBudget(0)
strOrg = &quot;&quot;
Case 2
strFund = arrBudget(0)
strOrg = arrBudget(1)
Case Else
End Select 'Select Case intCount

Tammy Thank you for your help.

Tammy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top