473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

how to parse a string

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


Nov 12 '05 #1
9 5646
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<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

What version of Access?

Nov 12 '05 #2

"Salad" <oi*@vinegar.com> 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<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

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
I have access version 2002

thanks

"Salad" <oi*@vinegar.com> 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<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

What version of Access?

Nov 12 '05 #4
"Danny" <da********@hotmail.com> wrote in message news:<NG**********************@news4.srv.hcvlny.cv .net>...
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


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
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" <ja******@oakland.edu> wrote in message
news:a6**************************@posting.google.c om...
"Danny" <da********@hotmail.com> wrote in message news:<NG**********************@news4.srv.hcvlny.cv .net>...
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


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
rkc

"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> 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
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<Tm*****************@news01.bloor.is.net.cabl e.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

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
"James Fortune" <ja******@oakland.edu> wrote in message
news:a6**************************@posting.google.c om...
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message

news:<Tm*****************@news01.bloor.is.net.cabl e.rogers.com>...
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
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> 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:

<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
Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and I need to parse strings that look similar to the one below. All 5 rows will make up one string. I have a form where a use can copy/paste data like what you...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
2
by: rajarshi.guha | last post by:
Hi, recently having discovered ElementTree I'm stumped by a very simple problem, which I can't find the answer to. I have some XML in a string object. Now the parse() method of ElementTree takes...
3
by: SharpCoderMP | last post by:
i've run into some trouble using data from xml inside my app. the scenario is simple. input data looks more or less like this: <item> <name>MyName</name> <somefloat>11.5</somefloat> </item> ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
4
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Hi folks, Boolean.Parse("true") returns true, but what if you need to parse something such as - Boolean.Parse("(false && false) || (false || (true && true))"); Does something exist in the...
2
by: semomaniz | last post by:
I have a page set where i get data in following format 41128°°8/20/2007 12:00:00 AM°11:00°True These data are stored in an arraylist name d. Then i use following code to split the data ...
2
by: Mika M | last post by:
Hi, Just for fun I'm trying to parse my GPS position string using C# 2005. When my code is trying to parse latitude string to double value like... double.Parse(items); // items = "6215.1058"...
2
by: hsachdevah | last post by:
Hi, I developed an application for my study project to do some mathematical calculations. In this application, I am reading from a text file which contains some numbers. Now the problem is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.