473,385 Members | 1,375 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,385 software developers and data experts.

how to count a particular symbol in a file

17
hai frnds i am doing my project in VB. i have to count some character sin a file.

for ex

nivaz is new******* here
so plz help him******ok



got that, from that i have to count each and every star in a line and also have to display that in a text box. how to do this help me guys. send ur replys, view to my inbox. thanks
Feb 16 '08 #1
3 1443
VBWheaties
145 100+
hai frnds i am doing my project in VB. i have to count some character sin a file.

for ex

nivaz is new******* here
so plz help him******ok



got that, from that i have to count each and every star in a line and also have to display that in a text box. how to do this help me guys. send ur replys, view to my inbox. thanks
You want the split function. This will return an array. From an array, you can count how many values there are using UBOUND.

Example:

Expand|Select|Wrap|Line Numbers
  1. Dim MyString As String 
  2. Dim Astericks
  3. Dim AsterickCount 
  4.  
  5. MyString = "This is my mother****ing string!!!"
  6.  
  7. Astericks = Split(MyString, "*") 
  8.  
  9. AsterickCount = UBOUND(Astericks)
  10.  

On the other hand, you probably havent learned that function (Split or Ubound).
In that case, you will need to iterate each character in a loop.
I'm certain you were taught Instr and/or Mid.

so..

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim MyString As String 
  3. Dim iCount as Integer
  4. Dim sChar As String 
  5.  
  6. For i = 1 to Len(MyString) 
  7.    sChar = MID(MyString, i, 1) 
  8.    If sChar = "*" Then 
  9.       iCount = iCount + 1
  10.    End If 
  11. Next i 
  12.  
  13. msgbox "There are " & iCount & " astericks in my string!" 
  14.  
  15.  
Feb 19 '08 #2
kadghar
1,295 Expert 1GB
...
I'm certain you were taught Instr and/or Mid.
...
I think Instr could be faster than Mid:

Expand|Select|Wrap|Line Numbers
  1. Dim i As Long
  2. Dim MyString As String 
  3. Dim iCount as Integer
  4. Dim sChar As String 
  5. do
  6.     i=instr(i+1,mystring,schar)
  7.     if i = 0 then exit do
  8.     icount = icount+1
  9. loop until i > len(mystring) 'this is just in case the last character of the string is sChar.
  10. msgbox "There are " & iCount & " " & sChar & " in my string!" 
  11.  
Anyway, using split and ubound will make things easier and faster than any of this two.
Feb 19 '08 #3
kadghar
1,295 Expert 1GB
Anyway, using split and ubound will make things easier and faster than any of this two.
My mistake, it'll only make things easier. Instr is the fastest, then Mid, and split-ubound is the slowest way (see, i've got so much free time =P):

Expand|Select|Wrap|Line Numbers
  1. Sub TimeTest()
  2. Dim a() As String
  3. Dim str1 As String
  4. Dim i As Long
  5. Dim t(2) As Single
  6. Dim r(2) As Long
  7.  
  8. str1 = string(250000,"*")
  9.  
  10. t(0) = Timer
  11. a = Split(str1, "*")
  12. r(0) = UBound(a)
  13. t(0) = Timer - t(0)
  14.  
  15. t(1) = Timer
  16. For i = 1 To Len(str1)
  17.    If Mid(str1, i, 1) = "*" Then
  18.       r(1) = r(1) + 1
  19.    End If
  20. Next i
  21. t(1) = Timer - t(1)
  22.  
  23. t(2) = Timer
  24. i = 0
  25. Do
  26.     i = InStr(i + 1, str1, "*")
  27.     If i = 0 Then Exit Do
  28.     r(2) = r(2) + 1
  29. Loop Until i > Len(str1)
  30. t(2) = Timer - t(2)
  31. MsgBox t(0) & "  -  " & r(0) & Chr(13) _
  32.         & t(1) & "  -  " & r(1) & Chr(13) _
  33.         & t(2) & "  -  " & r(2) & Chr(13)
  34. End Sub
Note : Of course the Split method could be faster if there are only a few * in the string, this will make the array smaller and faster as well. while the MID will be as fast-slow as it is. Anyway, i'd say the Instr is the more convenient.
Feb 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: upendrao | last post by:
Hi, I have a requirement in which I need to count the number of file descriptors associated with a file. To be more clear...A file can be opened by several processes say in read only mode. I would...
3
by: Ney André de Mello Zunino | last post by:
Hello. I would appreciate if somebody could assist me with an error message I am getting from the Microsoft WinDbg 6.3 debugging tool. I am not familiar with it, since I have just downloaded it....
9
by: junaidnaseer | last post by:
ok I know I posted this question previously and then I got a reply and I realized that I had asked a really dumb question but now I realize that my question wasn't that dumb at all ! I had asked...
4
by: max | last post by:
I am beginning to learn sql and need some help. I have a table of customers with their addresses. Let's say I want to run a query returning the number of customers whose last name is "Smith" by...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
3
by: javanooby | last post by:
Hi, I am having problems with this bit of code: public class main { public class readAccounts { reader1 r = new reader1();
1
by: Justin Johnson | last post by:
Hello, I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler (VisualAge C++ Professional / C for AIX Compiler, Version 6). I run configure and make, but makes fails with undefined...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
1
by: Derek Hart | last post by:
I wish to get a count of nodes in an xml file in vb.net, so I am using the following, as a simple example: MyCount = xmlNodePart.Current.Select.//MyNode/Record).Count I saw in some xml...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.