473,765 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting specific char's in a string

MLH
MyString = "All men are created equal"
Debug.PrintLen( MyString)

Now that's easy. But how about just counting
the letter "e"? Or, if I were curious to know
how many commas were in the string. How
might one do this in a query?

Suppose a table had single string field with
100 records, each containg a sentence
from a textbook. What would the SQL look
like in a query that listed all 100 sentences
and the number of e's appearing in each
sentence?
Mar 4 '07 #1
4 23613
Replace the character with nothing, and the difference in length is the
count of the character instances.

The query would be something like this:
SELECT Len([MyField]) - Len(Replace([MyField], ",", "")) AS CountOfCommas
FROM Table1;

This should work in Access 2002, 2003, and 2007.
It won't work in A97 or earlier.
In A2000, you may strike this problem:
http://support.microsoft.com/kb/225956

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"MLH" <CR**@NorthStat e.netwrote in message
news:rq******** *************** *********@4ax.c om...
MyString = "All men are created equal"
Debug.PrintLen( MyString)

Now that's easy. But how about just counting
the letter "e"? Or, if I were curious to know
how many commas were in the string. How
might one do this in a query?

Suppose a table had single string field with
100 records, each containg a sentence
from a textbook. What would the SQL look
like in a query that listed all 100 sentences
and the number of e's appearing in each
sentence?
Mar 4 '07 #2
Look for InStr in the help file

"MLH" <CR**@NorthStat e.netwrote in message
news:rq******** *************** *********@4ax.c om...
MyString = "All men are created equal"
Debug.PrintLen( MyString)

Now that's easy. But how about just counting
the letter "e"? Or, if I were curious to know
how many commas were in the string. How
might one do this in a query?

Suppose a table had single string field with
100 records, each containg a sentence
from a textbook. What would the SQL look
like in a query that listed all 100 sentences
and the number of e's appearing in each
sentence?

Mar 4 '07 #3
Hi -

This will work in A97.

Function StrCount(ByVal TheStr As String, theItem As Variant) As Integer
'------------------------------------------------------------------
' Purpose: Counts the numbers of times an item occurs
' in a string.
' Coded by: raskew
' Arguments: TheStr: The string to be searched.
' TheItem: The item to search for.
' Returns: The number of occurences as an integer.
'
' Note: To test: Type '? StrCount("The quick brown fox jumped over
' the lazy dog", "the") in the debug window.
' The function returns 2.
'------------------------------------------------------------------
Dim i As Integer
Dim j As Integer
Dim placehold As Integer
Dim strHold As String
Dim itemhold As Variant

strHold = TheStr
itemhold = theItem
j = 0

If InStr(1, strHold, itemhold) 0 Then
While InStr(1, strHold, itemhold) 0
placehold = InStr(1, strHold, itemhold)
j = j + 1
strHold = Mid(strHold, placehold + Len(itemhold))
Wend
End If
StrCount = j
End Function

HTH - Bob

Jeff Smith wrote:
>Look for InStr in the help file
>MyString = "All men are created equal"
Debug.PrintLen (MyString)
[quoted text clipped - 10 lines]
>and the number of e's appearing in each
sentence?
--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 5 '07 #4
MLH
Totally elegant
Mar 6 '07 #5

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

Similar topics

8
10407
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0; else{ s++; return 1 + count_length(s);
6
3111
by: Dale Atkin | last post by:
As part of a larger project, I need to be able to count the number of lines in a file (so I know what to expect). Anyways, I came accross the following code that seems to do the trick, the only thing is, I'm not 100% sure what it is doing, or how. #include<iostream> #include<fstream> main(int argc, char *argv) {
4
4074
by: sun6 | last post by:
this is a program counting words from "text_in.txt" file and writing them in "text_out.txt". it uses binary tree search, but there is an error when i use insert () thanks for any help #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
7
4807
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
7
2866
by: zets | last post by:
I need a macro for counting the bits in the odd positions of a given input (of any type, char, pointer, int, struct, whatever). Is there any clever way I could not think of, to do it efficiently? #define COUNT(num, count) \ do { \ unsigned char *safe ## num ## count = (unsigned char *)(&(num)); \
5
7704
by: andy.lee23 | last post by:
hi im having trouble counting lines in a text file, i have the following code int node1, node2, i; char name; float value; ifstream fin; fin.open(OpenDialog1->FileName.c_str()); i=1;
1
1450
by: gisleyt | last post by:
I'd like to use the index-function to retrieve a certain char in a string, but i need it to start counting from the last char. E.g. in the string. Foo-bar, foo foo-bar I want the latter hyphen to be returned when i do a index for '-'. Is there any built in function for this, I don't want to reverse every string just to do this.
6
1990
by: Matt Chwastek | last post by:
Anyone who can help, I am curretnly attempting to write some code that will allow iteration using a vector<intfrom the highest possilbe degree of a combination of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering of element containing the same number of ones is not important to the code, just the fact that the highest number of ones are iterated first. I would prefer not to use a binary string as eventually the
3
2788
by: majna | last post by:
I have character counter for textarea wich counting the characters. Special character needs same place as two normal characters because of 16-bit encoding. Counter is counting -2 when special character is added like some language specific char. How to count specials like 1 char? tnx
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.