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 3 1443
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: -
Dim MyString As String
-
Dim Astericks
-
Dim AsterickCount
-
-
MyString = "This is my mother****ing string!!!"
-
-
Astericks = Split(MyString, "*")
-
-
AsterickCount = UBOUND(Astericks)
-
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.. -
Dim i As Integer
-
Dim MyString As String
-
Dim iCount as Integer
-
Dim sChar As String
-
-
For i = 1 to Len(MyString)
-
sChar = MID(MyString, i, 1)
-
If sChar = "*" Then
-
iCount = iCount + 1
-
End If
-
Next i
-
-
msgbox "There are " & iCount & " astericks in my string!"
-
-
...
I'm certain you were taught Instr and/or Mid.
...
I think Instr could be faster than Mid: -
Dim i As Long
-
Dim MyString As String
-
Dim iCount as Integer
-
Dim sChar As String
-
do
-
i=instr(i+1,mystring,schar)
-
if i = 0 then exit do
-
icount = icount+1
-
loop until i > len(mystring) 'this is just in case the last character of the string is sChar.
-
msgbox "There are " & iCount & " " & sChar & " in my string!"
-
Anyway, using split and ubound will make things easier and faster than any of this two.
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): - Sub TimeTest()
-
Dim a() As String
-
Dim str1 As String
-
Dim i As Long
-
Dim t(2) As Single
-
Dim r(2) As Long
-
-
str1 = string(250000,"*")
-
-
t(0) = Timer
-
a = Split(str1, "*")
-
r(0) = UBound(a)
-
t(0) = Timer - t(0)
-
-
t(1) = Timer
-
For i = 1 To Len(str1)
-
If Mid(str1, i, 1) = "*" Then
-
r(1) = r(1) + 1
-
End If
-
Next i
-
t(1) = Timer - t(1)
-
-
t(2) = Timer
-
i = 0
-
Do
-
i = InStr(i + 1, str1, "*")
-
If i = 0 Then Exit Do
-
r(2) = r(2) + 1
-
Loop Until i > Len(str1)
-
t(2) = Timer - t(2)
-
MsgBox t(0) & " - " & r(0) & Chr(13) _
-
& t(1) & " - " & r(1) & Chr(13) _
-
& t(2) & " - " & r(2) & Chr(13)
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
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: ...
|
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();
|
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...
|
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)...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |