Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




Counting The Number Of Times A String Appears In Another String?


I have a string read from a text file, and I would like to fine the number of times a URL was used in it.

How would I go about doing this?

Thanks.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Counting The Number Of Times A Character Appears In A String
I am trying to find a way of counting how many times each character apears in a text string (will always be one a one word string)

The reason is to validate a password where a character cannot appear more than so many times, set by a paramater (pwMaxRepeat)

So the idea is for each character, count how many times it appears and if that count is greated than pwMaxRepeat then display an "Invalid Password" MsgBox

All help appreciated.......

Counting No. Of Times One String Appears Within Another
Hi.

Subject says it all really. How can i write a function to count the number of times a string such as "moo" appears in another string, such as "cows go moo a lot. Do you moo? I can moo. Everybody must moo like I moo, or the moo monster will moo at you!" which in this case is 7.

Thanks for your assistance

Kind regards,

------------------
- Chris
chris.kilhams@btinternet.com

Counting No. Of Times One String Appears Within Another
Hi.

Subject says it all really. How can i write a function to count the number of times a string such as "moo" appears in another string, such as "cows go moo a lot. Do you moo? I can moo. Everybody must moo like I moo, or the moo monster will moo at you!" which in this case is 7.

Thanks for your assistance

Kind regards,

------------------
- Chris
chris.kilhams@btinternet.com

Number Of Times ; Appears In String
how do i find out the number of times ; in a string
something like

email1; email2; email3

Counting The Number Of Times A Value Appears In An Array
Hi guys,

Apologies if this is already answered elsewhere, but I couldn't find it in searches.
I would appreciate it if someone could give me some general advice on this problem.
I'm pulling a number of records from a SQL database, and putting them in a multidimensional, dynamic array. What I need to do now, is somehow page through the array, and count the total number of times each specific value appears in it. I tried a few things with For Next loops, but unfortunatly my experience of arrays has been quite limited so far, so I'm not really 100% sure what I should be doing, and I've managed to confuse myself

Some advice on the best way to do this would be much appreciated.

Many thanks.

Whats A Quick Way To See How Many Times A String Appears In Another String?
"how Hello how are you how are you how"

How can you tell that 'how' appears in this string 4 times?

How Many Times A Character Appears In A String
Hi, I'm sure this must be an easy one but its got me baffled

I want to know how many times a comma appears in a string, i.e

abc,def,hij,klm,nop,qrs

number returned would be 5

Replacing A String Ever X Number Of Times?
i know the replace function in VB, but i was wondering if there was a
(Very efficent) way to replace a certain number of an occurance in a string

for example:
let's say you have this data:

"this;is;the;data;to;read;in;"

would there be a way to quickly replace every 3rd time a ; is found with a |
so it would become

this;is;the|data;to;read|in;

i'm looking for a way that wouldn't 'LOOP' through the string, and would possibly use the 'replace' function (since it's pretty efficent)
thanks

PS: if you're interested in why this has to be so efficent, i am reading in tab delimited files and i'm trying to build a web table based on this data. And i'm replacing the tabs with <TR><TD> or <TD> tags based on if it's a new row, or an existing row....that's why it has to be efficent, because a user is not going to wait forever for it to build the table
thanka!

How To Tell A String To Print Out X Number Of Times?
I need to do the following:
Make an inputted string be returned (as a single string) as many times as there are characters in the inputted string. I can build this like so:

Public Function printstring(ByVal str1 As String) As String
Dim i As Double
Do Until i = str1.Length
Console.WriteLine(str1)
i = i + 1
Loop
End Function

But, how would I accomplish this same thing only returning 1 single string instead of looping it to return x number of strings?

Count The Number Of Times That A Char Is In String
Does vb have a function that will count the number of occurances of a given character in a string or do you have to just use a bunch of instr?

Select Maximum Number Of Times Date Appears.
Ok, I'm pretty new to this stuff - but I need a bit of help.
Here is my query so far:

SELECT Collars.Date, Count(Collars.Date) AS [CountofDate]
FROM Collars
GROUP BY Collars.Date
HAVING max(countofdate);

Hold on, maybe I've got it wrong.
What I want to do is, select the maximum number of jobs done in a day (each job has its own entry, so there are multiple entries of the same date, I want to count how many times each date is there - and then select the max from this) how do I do this in one query? I can't remember, is there a way to create a temporary table? In which case - how do I do that for what I need?
Thanks for your help.

Counting Number Of Letters In String
I'm not sure what the syntax is because I've not been programming VB for very long (in fact I've only been programming since september last year!)

But what I want to do is, allow the User to type in some text (its one word) then for the Program to give an output depending on the number of letters in that word.

I imagine the structure would be something like this,

Command button is pushed, the text in the text box is assigned to a string variable, then I'm not quite sure so I can't start to guess at syntax, and from that the number of characters in that string are converted to an integer. I imagine this is going to involve nested For loops.

from that I can then use the Integer to give the output based on the value of the Integer using If statements (it will only be a 4 - 8 charater word for the most part so that won't take to long. (note the output is not the same as the Integer.)

Counting The Number Of Results When Searching A String
Hi,

I have another string handling question for the group, since you have all been so helpful in the past. Thank you.

Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the substring appears in the mainstring

e.g.

mainstring = “The man walked through the park, the man was happy”
substring = “man”
Count = 2 (Because ‘man’ appears twice)

Now, to me this sounds like a very simple thing to do, (It is, right?) but I have been working on it for 3 hours now and it’s starting to make my brain hurt.

The problem I am having (Take the main string I mentioned earlier, searching for “Man”) is moving the searcher to the right position. The second “man” in my mainstring is at position 38. If the searcher gets to position 10 it finds it at position 38 (so count = 2), if it moves to position 15, it finds it at position 3 (count = 3), and as it continues until it gets past the position it counts it every time it sees it.

I’ve tried coding so that every time a position is duplicated, count = count + 1 – 1. It works if the position occurs twice, but anymore than that counts it! I think somehow I need to store the previous position count in a variable, then check it with the current one and change the count number accordingly. But i can't seem to get there!

I think my code is nearly there, it just needs a little something extra. Can anyone help please?

Here is my code (I popped lots of msgboxes in there to try and help me see where I was going wrong – to no avail!):



Dim mainstring As String 'Declare Variables
Dim searchstring As String
Dim searcher As Integer
Dim count As Integer
Dim Startpoint As Integer

mainstring = " " & txtMain.Text 'Initialise variables
searchstring = txtSearch.Text

Do

Startpoint = Startpoint + Len(searchstring) + 1 'Calculate place to start searching mainstring
MsgBox ("Startpoint = " & Startpoint)

searcher = InStr(Startpoint, mainstring, searchstring)
'Search mainstring for searchstring, starting from calculated start point.
MsgBox searcher

If (searcher > 0) Then
'If position of searchstring in mainstring is > 0 then

count = count + 1 'add 1 to counter
MsgBox ("+ 1 to count")

ElseIf searcher = searcher Then
'If position or searchstring is found twice, then

count = count + 1
count = count - 1 'Minus 1 from counter
MsgBox ("-1 from count")
End If

MsgBox ("Count = " & count)
'Display position of found word

Loop Until searcher = 0 'Loop until there are no more words

End Sub



Thanks!

Cassandra

String - Counting The Number Of Line Breaks CHR(13)
I have a string with some text and a few line breaks.

How can I count the number of line breaks chr(13) in this string? Any example code?

Counting Number Of Results In A String Search
Hi,

I have another string handling question for the group, since you have all
been so helpful in the past. Thank you.



Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the
substring appears in the mainstring



e.g.

mainstring = "The man walked through the park, the man was
happy"

substring = "man"

Count = 2 (Because 'man' appears twice)



Now, to me this sounds like a very simple thing to do, (It is, right?) but I
have been working on it for 3 hours now and it's starting to make my brain
hurt.



The problem I am having (Take the main string I mentioned earlier, searching
for "Man") is moving the searcher to the right position. The second "man"
in my mainstring is at position 38. If the searcher gets to position 10 it
finds it at position 38 (so count = 2), if it moves to position 15, it finds
it at position 3 (count = 3), and as it continues until it gets past the
position it counts it every time it sees it.



I've tried coding so that every time a position is duplicated, count = count
+ 1 - 1. It works if the position occurs twice, but anymore than that
counts it! I think somehow I need to store the previous position count in a
variable, then check it with the current one and change the count number
accordingly. But i can't seem to get there!



I think my code is nearly there, it just needs a little something extra.
Can anyone help please?



Here is my code (I popped lots of msgboxes in there to try and help me see
where I was going wrong - to no avail!):



Dim mainstring As String 'Declare Variables

Dim searchstring As String

Dim searcher As Integer

Dim count As Integer

Dim Startpoint As Integer



mainstring = " " & txtMain.Text 'Initialise variables

searchstring = txtSearch.Text



Do

Startpoint = Startpoint + Len(searchstring) + 1
'Calculate place to start searching mainstring

MsgBox ("Startpoint = " & Startpoint)

searcher = InStr(Startpoint, mainstring, searchstring) 'Search
mainstring for searchstring, starting from calculated start
point.

MsgBox searcher



If (searcher > 0) Then 'If position of searchstring in
mainstring is > 0 then

count = count + 1 'add 1 to counter

MsgBox ("+ 1 to count")

ElseIf searcher = searcher Then 'If position or searchstring is
found twice, then

count = count + 1

count = count - 1 'Minus 1 from counter

MsgBox ("-1 from count")

End If



MsgBox ("Count = " & count)

'Display position of found word



Loop Until searcher = 0 'Loop until there are no more words



End Sub



Thanks!



Cassandra

Counting Number Of Times Certain Numbers Come Together
Hello,

I am writing a program that counts how many times certain pairs of numbers come in together. What my program will do is read from a text file and count. Just...I don't know where to begin.

The text file will look something like this:
1,9,14,24,10
7,12,14,24,12
etc

I need the program to read each line, and count how many times certain pairs of numbers come together (in this case, 14 and 24 come in together twice) and print out a report. Can anybody point me in the right direction? :-)

Extracting A String Contained Within A String/arithmetic On Times
Hi All !
I am pretty new to the world of VB so please forgive me if my questions are a little silly.

Basically i am building a radio playlist interface...and i need to know a couple of things.

Firstly i am using a combo box to select a track which appears in this format Track 1 ( 4.32) and then then it appears in the list box(playlist) when i click a button.
Though i also have 2 list boxes to the right of the playlist and in the playlist only 'Track 1' should appear while in the next listbox should appear the start time of the track (eg 0.00 for start of program, or 32.11 for 32 minutes and eleven seconds into the program), and in the other list box should appear the duration of the track which appeared in the initial combolist alongside trackname,
I know i have to extract a string from a string..but i am clueless as to how to do it and how to perform arithmetic on times !!!
Anyone got any suggestions ?
I know it's a big ask but any help would be much appreciated.

Thanks

HarpoonFace

Counting The Number Of Times A Certain Word Occurs In A Flexgrid Column?
if i had ONE column in a flexgrid, and that column had a few different words occurring randomnly, eg:


Code:
chicken
fish
chicken
chicken
fish
chicken
fish
fish
chicken
fish
chicken
.
.
.


like that.... is there a way to count the number of times that the word 'fish' occurs and the number of times 'chicken' occurs? and then send these numbers to two separate labels?

thanks

MS Excel - How Many Times A Value Appears
Anybody out there good with VBA and Excel? I am trying to search a sheet in a workbook (Sheet1), find out how many times particular values exists (int 1 to 54), then create a new workbook with a list of the values checked for and how many times they appeared.
Been googling this, but everything I have found just confused me more, never done much with Office before. Thanks in advance for any help!!

By the way, I am using Excel 2003 if that matters...

Amount Of Times A String Changes Per Second?
i have some code that works by going though webpages, but i need to have it tell you how many it is doing per second.
the string is called Page

just need some code that gives the output at label3 and updates every second . thanks any help will be great

Times CHAR Is In String
Is there a function that would tell me how many times a certain charecter is in a string? I could do a loop with INSTR but was wondering if there was an easier way

How Many Times Does A Char Appear In A String?
Hello,

I would like to count how many times a char (for instance "") appears in a string, I've tried sevral ways (tried using a while with instr and a place pointer that I advance) but none have worked, any ideas?

Thanks!

(VB6)string To Number And Number To String Convert
Hon'ble gurus,
I am using VB6 and MsAccess. Can I change the types of the following? If so, then
how can I change a string to number or vice versa?
AND
how can I change a date to text and text to date?
pkb_pkb

pkb_pkb

Counting In A String
Ok, this is an odd one, is it possible to count how many of any character there is in a specified string.


Code:
strTest="1,2,3,4,5,6"
To be able to count how many "," there are in strTest.

Thanks in advance!

~ That Dude Behind Fewi ~

String Counting
How do you count the number of a certian letter in a string????


2nd, you know how in certian programs like calculator. the command buttions are rounded off on the corners. how do you get the VB command buttions like this?

Counting How Many * 's Are In A String
Hi,

I have a string which has a series of 1 digit numbers which are separated by *'s how can I count how many *'s there are in the string?

Thanks in advance

Counting A String
I have a number such as 3 and then the second user entered number must be 3 digits long.... like 3 and then 123.
so how can i validate to make sure value 2 is the right number of characters. i thought there was a "count" function that would count the characters in a string, but i can't find it. please help
thanks!

Repeating A Mid$ Statement Numerous Times Througout A String?
Alright, here is what I would essentially like to do.

Say I have a source from an HTML page. I would like to be able to get every Term in between '<B><I>' and '</I></B>'. However, the problem lies with the mid statement.

Obviously, I could do something like the following:


Code:
x = InStr(myString, "<B><I>")
y = InStr(myString, "</I></B>")
important = Mid$(myString, x + 6, y - x - 6)
lstStatements.AddItem important

However, that will continue to get the same statement over and over, because it has no way of bypassing what it has already gotten.

Is there any way I can do this?

For further clarification -- Lets say I have the following page.


Code:
<HTML>
<BODY>
Hello, Welcome to my Fake Page.<BR>
<BR>
Here are some special Announcements For you to gawk at!<BR>
<BR>
1. <B><I>I am Special</I></B><BR>
2. <B><I>This Page is Cool</I></B><BR>
3. <B><I>I like mascots</I></B><BR><BR>

Please check back often for updates.
I would like to somehow get all three of the statements, however, it would be changing numbers, so I could not denote a For() statement.

Help?

Finding How Many Times The Same Word Appears In A Text File? THANKS JOACIM
Hello,

I have a .txt file and it loads into a TextBox on my program. Once loaded I need a cmd button to click that will tell me how many times each word that exists in the textbox is.

For example.

Textbox.Text = Hello, My name is Stilekid007, what is your name?

RESULTS WOULD BE:

1 Hello
1 My
2 Name
2 Is
1 Stilekid007
1 What
1 Your
-------

Does anyone know if this can be done in VB 6.0?

Thank you so muc for any replys!
Stilekid007

Counting Contents Of String
Hello,

I need help with counting the contents of a string.

Here is an example string: 1-UR 2--- 3LU-. I need to count the number of letters in the code. the letters can be L, U, R in that order. If they are not present they are replaced by a "-". I only need the total number of letters in the string.

I tried using a worksheet countif function but that won't do. Here is my failed code.

Sub option_counter()
Dim RC As Single
Dim PS As Range
Dim A As Single
Dim cll As Range

RC = Cells(Rows.Count, 9).End(xlUp).Row
Set PS = Range(Cells(2, 9), Cells(RC, 9))

For Each cll In PS
If Not IsEmpty(cll) And IsEmpty(cll.Offset(0, 1)) Then
A = WorksheetFunction.CountIf(cll, "L") _
+ WorksheetFunction.CountIf(cll, "U") _
+ WorksheetFunction.CountIf(cll, "R")
cll.Offset(0, 1).Value = A
End If
Next cll

End Sub

Thank You

Counting Characters In A String
Hi, i was wondering if there is a way to count how many 1's in a string like "1109872981276398162371221369127361231".

Counting Instances Of A String?
OK, take this example.

I have a textbox, multiline on and all that...
I have a button, it reads the textbox and counts all the instances of the word... "explosion"
In the textbox, I want it so everytime the word "EXPLOSION" or any captial varient is typed, it is changed to "explosion".

My 2 Questions are: How do I count the number of instances of a string. and, how do I test for EXPLOSION and turn it in to explosion...

I have googled a bit, looked in the MSDN, even did some searching the the in program help...

!! Counting Words In A String !!
I have got a problem in VB6!! Please help

If I have a string
eg: The cat sat on the mat
I want to count the number of words in the string

Please help
Thanks

String Counting And Taking ...(Pls See)
For example, we get a string of chars from a place(like input box) Can we just take out any portion of the string of chars...? Like the 1st char or the 2nd char or even both the char... Can?

Counting A Char In A String
Is there a easy way of counting how many times a certain char repeats itself in a string?

Fake Code to explain:
CountChar(a, anaconda)

this would return "3"...

Is this possible?

Counting Characters In A String
Do I have to make a loop with InStr or is there a function I'm unaware of?

Counting Letters In A String
I'm trying to figure out how to count how many times a letter appears in a string, ive been trying various things with no luck. I know its going to turn out to be something simple that I'm just forgetting to do, but any help would be appreciated.

Counting The Images In A String?
Is there a way to either count the URLs or the Images in a string?

I have the source of a page saved to a string, through Inet, but would like to see the number of times a specific string appears.

Counting A String Length
Hi guys, I want to be able to count how many characters a string is holding. I need to be able to delete any strings that are longer than an expected string length say 15 character strings pass and is read, but a 16 character string will be deleted or bypassed. The following code is something that I think westconn1 showed me.


Code:
pos = 0
str = "X-1869Y+1515D01"
number = 2
letter = 1


Range("A1").Value = Mid(str, letter, number)
Range("B1").Value = Mid(str, 3, 4)
Range("C1").Value = Mid(str, 7, pos + 2)
Range("D1").Value = Mid(str, 9, pos + 4)
Range("E1").Value = Mid(str, 13, 14)


This is the longest string I need to work with. At the moment the code just looks at the postion and writes the values it sees in the next cell across, however if I count the string in a cell and determine that the string is only say 5 characters long then the above code writes all the values in the wrong cells. In effect I want to read the string find out how long it is write code to deal with the current length of the string and place the values in the right cells. The next stage is to take the string and find only letters and count how many numeric values are following before it strkes a letter again, then count the numeric values until a letter is encounted again. Sound weird! I thought so as well until I tried running a program through a PIC to sort the values out and found that data is easily corrupted after it returns to the PC.

Counting How Many Occurance In A String
How can I find out how many "-" are in a string?

Counting A Character In A String?
I have a string of "146~200~189~223~188". Is there a VB6 function that can count how many '~' occur in that string or do i have to create my own function?

Kind regards,
Ohn Ic

Counting Occurances In A String
I have a string that contains text. How do I count the number of occurances of one string in the other.

e.g. How do I count how many times "word" is in "These are my word examples. My word on it."

Counting String Length
I need help on a simple decoding program.

I figured that VB can store the length of a string, so therefore it must be able to display a letter in that string based on the number you put into it, e.g. You have a sentence "Hello" it's length is 5, you enter number 3 and it displays letter "L".

This also has to apply to spaces, so if you enter the string "I am" it's length will be 4, if you enter the number 2, then it returns a space.

I'm wondering if anyone has any idea on how this would be done, any help is appriciated.

Many thanks.

Counting Charecters In A String...HELP
I need a way for vb to count the number of charecters in a string..so something basic like

TextBox1.Text charecters = ???

is there a len function or something to do this?

i just want it to tell me the number of charecters/spaces/whatevers in the box. please help quik!

Array Counting Via A String
Dear VB users,

Is it possible to put a array name in a variable and then using the function Ubound?

For example:

Dim MyArray As String
ReDim Preserve G01(1)
MyArray = "G01"
MsgBox UBound(MyArray)

Array G01 exist, only it is not possible to get the number array via this way. I know “MsgBox UBound(G01)” is correct.

Knows someone the source code?

Nice regards,

Michelle.

Counting Characters In A String
Is there a VB function for counting the number of specific characters in a string?

e.g. How many i's in Mississippi

If not I guess it will have to be a loop.

Counting Occurance Of Each Character In A String.
I am trying to write a simple program to input a string and then output how many times each letter appears. This is harder than I thought. I know this is a VB forum, but maybe some C++ guys or even a VB guy can answer this. I poste din the C++ forum a few hours ago and haven't gotten any looks, more less responses so I am giving it a shot here.

For example, if someone inputs "I went to the store", I would then output

A:0
B:0
C:0
etc...
I:1
J:0
etc...

a:0
b:0
c:0
d:0
e:3
f:0
g:0
h:1
etc...
n:1
o:2
etc..
t:4
etc...
w:1
etc...

I hope that makes sense... I think I have firgured it out for the most part, but it's not working correctly. It prints 122 at the "z", which is the size of the frequency array instead of printing how many z's were entered if any.


Thanks!!


PHP Code:




#include <iostream>
using namespace std;

#include <cstring>
using namespace std;


void main(){

    const freqArrSize=122;
    const charArrSize=10000;
    int counter=0;
    int freqArray[freqArrSize]={0};//frequency that each letter occurs
    char charArray[charArrSize]={0};
    char theChar;

    cout << "Enter some text to calculate the frequency of each character: ";
    
    cin.getline(charArray,charArrSize,'
');


    //Count occurance of each letter and add it to the freq array
    for (counter = 0; counter < strlen(charArray); counter++){

        cout << static_cast<int>(charArray[counter]) << " ";

     ++freqArray[static_cast<int>(charArray[counter])];

    }//end for

     cout << endl << "UPPER CASE" << endl;

       //Print out all upper case letters
       for(counter = 65; counter < 91; counter++){

           theChar = static_cast<char>(counter);//convert int into to Letter
               
              cout << theChar << ":" << freqArray[counter] << endl;//print the Letter and how many times it occured
           
       }//end for
    
       cout << endl << "lower case" << endl;

       //Print out all lower case letters
       for(counter = 97; counter < 123; counter++){

           theChar = static_cast<char>(counter); //convert int into to Letter
               
              cout << theChar << ":" << freqArray[counter] << endl;//print the Letter and how many times it occured
           
       }//end for

}//end function main

Counting Elements In A String (Resolved)
What is an easy way to return the number of times that a certain character occurs within a string? I don't need to know the position.

For example, there are 3 A's in this string: "AAAbcde"

Thanks!

Counting Specific Chars In A String
It looks like there is no function for counting the amount of a certain character(s) in a string (or I am just dumb). Right now I've got this:


Code:
Public Function StrCount(String1, String2) As Long
For x = 1 To Len(String1)
If Mid(String1, x, Len(String2)) = String2 Then StrCount = StrCount + 1
Next x
End Function


But this seems to work too:

Code:
Public Function StrCount(String1, String2) As Long
Dim sCount() As String
sCount = Split(String1, String2)
StrCount = UBound(sCount)
End Function


But surely there is a VB function that I missed, or an API. There's a few string API functions, but none of them seem to do anything related to this...

Any Ideas?

Copyright © 2005-08 www.BigResource.com, All rights reserved