Connecting Tech Pros Worldwide Forums | Help | Site Map

how to parse a string

Danny
Guest
 
Posts: n/a
#1: Nov 12 '05
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<br>is<br>a<br>test
would return 3 for <br>

2. Parse a string into an array given a separation string like

this<br>is<br>a<br>test
so for <br> as delimiter
array(0) = this
array(1) = is
array(2) = a
array(3) = test

Thanks
Danny





Salad
Guest
 
Posts: n/a
#2: Nov 12 '05

re: how to parse a string


Danny wrote:
[color=blue]
> 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<br>is<br>a<br>test
> would return 3 for <br>
>
> 2. Parse a string into an array given a separation string like
>
> this<br>is<br>a<br>test
> so for <br> as delimiter
> array(0) = this
> array(1) = is
> array(2) = a
> array(3) = test
>
> Thanks
> Danny
>[/color]
What version of Access?

Danny
Guest
 
Posts: n/a
#3: Nov 12 '05

re: how to parse a string



"Salad" <oil@vinegar.com> wrote in message
news:Dbwkc.435$Hs1.38@newsread2.news.pas.earthlink .net...[color=blue]
> Danny wrote:
>[color=green]
> > 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<br>is<br>a<br>test
> > would return 3 for <br>
> >
> > 2. Parse a string into an array given a separation string like
> >
> > this<br>is<br>a<br>test
> > so for <br> as delimiter
> > array(0) = this
> > array(1) = is
> > array(2) = a
> > array(3) = test
> >
> > Thanks
> > Danny
> >[/color]
> What version of Access?
>[/color]

I use Access 2002

Why don't my posts show up sometimes?
This is second time I posted this.

Thanks


Danny
Guest
 
Posts: n/a
#4: Nov 12 '05

re: how to parse a string


I have access version 2002

thanks

"Salad" <oil@vinegar.com> wrote in message
news:Dbwkc.435$Hs1.38@newsread2.news.pas.earthlink .net...[color=blue]
> Danny wrote:
>[color=green]
> > 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<br>is<br>a<br>test
> > would return 3 for <br>
> >
> > 2. Parse a string into an array given a separation string like
> >
> > this<br>is<br>a<br>test
> > so for <br> as delimiter
> > array(0) = this
> > array(1) = is
> > array(2) = a
> > array(3) = test
> >
> > Thanks
> > Danny
> >[/color]
> What version of Access?
>[/color]


James Fortune
Guest
 
Posts: n/a
#5: Nov 12 '05

re: how to parse a string


"Danny" <dannywork5@hotmail.com> wrote in message news:<NGvkc.58860$Gd3.14093644@news4.srv.hcvlny.cv .net>...[color=blue]
> 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<br>is<br>a<br>test
> would return 3 for <br>
>
> 2. Parse a string into an array given a separation string like
>
> this<br>is<br>a<br>test
> so for <br> as delimiter
> array(0) = this
> array(1) = is
> array(2) = a
> array(3) = test
>
> Thanks
> Danny[/color]

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
Douglas J. Steele
Guest
 
Posts: n/a
#6: Nov 12 '05

re: how to parse a string


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" <jafortun@oakland.edu> wrote in message
news:a6ed3ce7.0405011945.518a6f7d@posting.google.c om...[color=blue]
> "Danny" <dannywork5@hotmail.com> wrote in message[/color]
news:<NGvkc.58860$Gd3.14093644@news4.srv.hcvlny.cv .net>...[color=blue][color=green]
> > 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<br>is<br>a<br>test
> > would return 3 for <br>
> >
> > 2. Parse a string into an array given a separation string like
> >
> > this<br>is<br>a<br>test
> > so for <br> as delimiter
> > array(0) = this
> > array(1) = is
> > array(2) = a
> > array(3) = test
> >
> > Thanks
> > Danny[/color]
>
> I haven't tested this much; it's not very elegant.
>
> Public Function CountSubstrings(strIn As String, strFind As String) As[/color]
Integer[color=blue]
> 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[/color]


rkc
Guest
 
Posts: n/a
#7: Nov 12 '05

re: how to parse a string



"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
news:Tm4lc.5982$4AC.4763@news01.bloor.is.net.cable .rogers.com...[color=blue]
> 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[/color]
occur[color=blue]
> 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[/color]

Cool. Unbelievably fast.







James Fortune
Guest
 
Posts: n/a
#8: Nov 12 '05

re: how to parse a string


"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<Tm4lc.5982$4AC.4763@news01.bloor.is.net.cabl e.rogers.com>...[color=blue]
> 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)
>[/color]

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
Douglas J. Steele
Guest
 
Posts: n/a
#9: Nov 12 '05

re: how to parse a string


"James Fortune" <jafortun@oakland.edu> wrote in message
news:a6ed3ce7.0405021556.410ab3a1@posting.google.c om...[color=blue]
> "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message[/color]
news:<Tm4lc.5982$4AC.4763@news01.bloor.is.net.cabl e.rogers.com>...[color=blue][color=green]
> > And if all you want is a function to tell you how many occurrencs of x[/color][/color]
occur[color=blue][color=green]
> > 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[/color][/color]
occurrence of[color=blue][color=green]
> > strFind with a zero-length string (""). It then calculates the[/color][/color]
difference in[color=blue][color=green]
> > 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)
> >[/color]
>
> 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.[/color]

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)




James Fortune
Guest
 
Posts: n/a
#10: Nov 12 '05

re: how to parse a string


"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<8rzlc.379779$2oI1.148425@twister01.bloor.is. net.cable.rogers.com>...[color=blue]
> 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).[/color]

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:

<Begin Paraphrase>
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.)
<End Paraphrase>

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
Matt
Guest
 
Posts: n/a
#11: Nov 12 '05

re: how to parse a string


Hello,

I was interested in your comment[color=blue]
> why I was having a problem outputting pdf information
> in binary mode after a string was used to
> store that information[/color]
I am running into a similar problem and wanted to know if there was a
way to properly restore the data to a byte array from the "storing"
string.

jafortun@oakland.edu (James Fortune) wrote in message news:<a6ed3ce7.0405032239.19d6e31c@posting.google. com>...[color=blue]
> "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<8rzlc.379779$2oI1.148425@twister01.bloor.is. net.cable.rogers.com>...[color=green]
> > 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).[/color]
>
> 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:
>
> <Begin Paraphrase>
> 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.)
> <End Paraphrase>
>
> 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[/color]
Closed Thread