434,660 Members | 1,945 Online
Need help? Post your question and get tips & solutions from a community of 434,660 IT Pros & Developers. It's quick & easy.

# how to parse a string

 P: n/a HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this
is
a
test would return 3 for
2. Parse a string into an array given a separation string like this
is
a
test so for
as delimiter array(0) = this array(1) = is array(2) = a array(3) = test Thanks Danny Nov 12 '05 #1
9 Replies

 P: n/a Danny wrote: HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this
is
a
test would return 3 for
2. Parse a string into an array given a separation string like this
is
a
test so for
as delimiter array(0) = this array(1) = is array(2) = a array(3) = test Thanks Danny What version of Access? Nov 12 '05 #2

 P: n/a "Salad" wrote in message news:Db**************@newsread2.news.pas.earthlink .net... Danny wrote: HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this
is
a
test would return 3 for
2. Parse a string into an array given a separation string like this
is
a
test so for
as delimiter array(0) = this array(1) = is array(2) = a array(3) = test Thanks Danny What version of Access? I use Access 2002 Why don't my posts show up sometimes? This is second time I posted this. Thanks Nov 12 '05 #3

 P: n/a I have access version 2002 thanks "Salad" wrote in message news:Db**************@newsread2.news.pas.earthlink .net... Danny wrote: HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this
is
a
test would return 3 for
2. Parse a string into an array given a separation string like this
is
a
test so for
as delimiter array(0) = this array(1) = is array(2) = a array(3) = test Thanks Danny What version of Access? Nov 12 '05 #4

 P: n/a "Danny" wrote in message news:... HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this
is
a
test would return 3 for
2. Parse a string into an array given a separation string like this
is
a
test so for
as delimiter array(0) = this array(1) = is array(2) = a array(3) = test Thanks Danny I haven't tested this much; it's not very elegant. Public Function CountSubstrings(strIn As String, strFind As String) As Integer Dim intCount As Integer Dim strTemp As String Dim intFound As Integer 'Count the number of strFind strings within strIn. CountSubstrings = 0 If Len(strIn) = 0 Then Exit Function If Len(strFind) > Len(strIn) Then Exit Function intFound = InStr(1, strIn, strFind, vbTextCompare) If intFound = 0 Then Exit Function intCount = 1 CountSubstrings = 1 'See if the string was found at the end of strIn If Len(strIn) = intFound - 1 + Len(strFind) Then Exit Function 'Chop off up to and including the found string 'Note: Add code here to catch contents for an array if desired strTemp = Right(strIn, Len(strIn) - intFound - Len(strFind) + 1) intFound = InStr(1, strTemp, strFind, vbTextCompare) Do While intFound > 0 intCount = intCount + 1 If Len(strTemp) = intFound - 1 + Len(strFind) Then CountSubstrings = intCount Exit Function End If 'Note: And code here strTemp = Right(strTemp, Len(strTemp) - intFound - Len(strFind) + 1) intFound = InStr(1, strTemp, strFind, vbTextCompare) Loop CountSubstrings = intCount End Function You'll want to index on intCount - 1 for a zero based array and I'd use a Sub instead of a Function to return the substring count and the array. James A. Fortune Nov 12 '05 #5

 P: n/a The Split function suggested by Chuck would seem to be more appropriate. And if all you want is a function to tell you how many occurrencs of x occur in y, the following does it: Public Function CountSubstrings(strIn As String, strFind As String) As Integer CountSubstrings = (Len(strIn) - Len(Replace(strIn, strFind, ""))) \ Len(strFind) End Function What this does is make a new version of strIn, replacing every occurrence of strFind with a zero-length string (""). It then calculates the difference in length between the length of the original string and the length of the modified string. Finally, it divides that difference by the length of strFind. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (No private e-mails, please) "James Fortune" wrote in message news:a6**************************@posting.google.c om... "Danny" wrote in message news:... HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this
is
a
test would return 3 for
2. Parse a string into an array given a separation string like this
is
a
test so for
as delimiter array(0) = this array(1) = is array(2) = a array(3) = test Thanks Danny I haven't tested this much; it's not very elegant. Public Function CountSubstrings(strIn As String, strFind As String) As Integer Dim intCount As Integer Dim strTemp As String Dim intFound As Integer 'Count the number of strFind strings within strIn. CountSubstrings = 0 If Len(strIn) = 0 Then Exit Function If Len(strFind) > Len(strIn) Then Exit Function intFound = InStr(1, strIn, strFind, vbTextCompare) If intFound = 0 Then Exit Function intCount = 1 CountSubstrings = 1 'See if the string was found at the end of strIn If Len(strIn) = intFound - 1 + Len(strFind) Then Exit Function 'Chop off up to and including the found string 'Note: Add code here to catch contents for an array if desired strTemp = Right(strIn, Len(strIn) - intFound - Len(strFind) + 1) intFound = InStr(1, strTemp, strFind, vbTextCompare) Do While intFound > 0 intCount = intCount + 1 If Len(strTemp) = intFound - 1 + Len(strFind) Then CountSubstrings = intCount Exit Function End If 'Note: And code here strTemp = Right(strTemp, Len(strTemp) - intFound - Len(strFind) + 1) intFound = InStr(1, strTemp, strFind, vbTextCompare) Loop CountSubstrings = intCount End Function You'll want to index on intCount - 1 for a zero based array and I'd use a Sub instead of a Function to return the substring count and the array. James A. Fortune Nov 12 '05 #6

 P: n/a "Douglas J. Steele" wrote in message news:Tm*****************@news01.bloor.is.net.cable .rogers.com... The Split function suggested by Chuck would seem to be more appropriate. And if all you want is a function to tell you how many occurrencs of x occur in y, the following does it: Public Function CountSubstrings(strIn As String, strFind As String) As Integer CountSubstrings = (Len(strIn) - Len(Replace(strIn, strFind, ""))) \ Len(strFind) End Function Cool. Unbelievably fast. Nov 12 '05 #7

 P: n/a "Douglas J. Steele" wrote in message news:... The Split function suggested by Chuck would seem to be more appropriate. And if all you want is a function to tell you how many occurrencs of x occur in y, the following does it: Public Function CountSubstrings(strIn As String, strFind As String) As Integer CountSubstrings = (Len(strIn) - Len(Replace(strIn, strFind, ""))) \ Len(strFind) End Function What this does is make a new version of strIn, replacing every occurrence of strFind with a zero-length string (""). It then calculates the difference in length between the length of the original string and the length of the modified string. Finally, it divides that difference by the length of strFind. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (No private e-mails, please) Very elegant. I like it a lot. I'll have to keep looking for ways to improve my existing functions. Is there an equally elegant solution to CountSubstrings for Access 97? Are there some nice API string functions that would make life easier for Access programmers. James A. Fortune Nov 12 '05 #8

 P: n/a "James Fortune" wrote in message news:a6**************************@posting.google.c om... "Douglas J. Steele" wrote in message news:... And if all you want is a function to tell you how many occurrencs of x occur in y, the following does it: Public Function CountSubstrings(strIn As String, strFind As String) As Integer CountSubstrings = (Len(strIn) - Len(Replace(strIn, strFind, ""))) \ Len(strFind) End Function What this does is make a new version of strIn, replacing every occurrence of strFind with a zero-length string (""). It then calculates the difference in length between the length of the original string and the length of the modified string. Finally, it divides that difference by the length of strFind. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (No private e-mails, please) Very elegant. I like it a lot. I'll have to keep looking for ways to improve my existing functions. Is there an equally elegant solution to CountSubstrings for Access 97? Are there some nice API string functions that would make life easier for Access programmers. I always add my own Replace and Split functions to all my Access 97 databases, so that it doesn't really become an issue. I'm not aware of any string-specific APIs (largely because strings are different between C and VB). -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (No private e-mails, please) Nov 12 '05 #9

 P: n/a "Douglas J. Steele" wrote in message news:<8r**********************@twister01.bloor.is. net.cable.rogers.com>... I always add my own Replace and Split functions to all my Access 97 databases, so that it doesn't really become an issue. I'm not aware of any string-specific APIs (largely because strings are different between C and VB). Thanks for the information. Here is a method for dealing with C/VB strings. Apologies in advance to Steven Roman if I interpret what he said incorrectly. The book gives a much more detailed look at what goes on. You can use the following "trick" suggested by Steven Roman in his book "Win32 API Programming with Visual Basic" ISBN: 1-56592-631-5: Terminology: BSTR - VB string (pointer to Unicode Array) The Unicode character array that is pointed to by a BSTR must be preceded by a 4-byte length field and terminated by a single null 2-byte character (ANSI = 0). LPSTR - long pointer to a null-terminated ANSI character array (VC++ string) LPWSTR - long pointer to a null-terminated Unicode character set with no embedded nulls (VC++ string) ABSTR - made-up term used for BSTR type character array pointing to an ANSI array s = StrConv(s, vbToUnicode) APIFunctionW s s = StrConv(s, vbFromUnicode) "What we are doing here is compensating for the shrinking of our BSTR to an ABSTR by expanding it first. Indeed, the first call to the StrConv function simply takes each byte in its operand and expands it to Unicode format. It doesn't know or care that the string is already in Unicode format." .... "Now, in preparation for passing the string to [APIFunctionW], VB takes this expanded string and converts it from Unicode to ANSI, thus returning it to its original Unicode state. At this point, [APIFunctionW] can make sense of it and do [its function]. Once the converting string returns from [APIFunctionW], VB "translates" the result to Unicode, thus expanding it with embedded null characters. We must convert the result to ANSI to remove the supererogatory padding." .... Special care is required in the case of Windows 9x since it behaves differently. (He goes on to explain how to handle that case also.) There must be several string functions that are used by Win32 to provide the OS with what it needs. BTW, Steven's explanation of how VB stores strings internally helped me discover why I was having a problem outputting pdf information in binary mode after a string was used to store that information. Thanks Steven. James A. Fortune Nov 12 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.