473,386 Members | 1,962 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,386 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 5651
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.