[RESOLVED] newbie here... need help on built-in functions...
rivate Sub Command1_Click()
Dim x As Integer, ng As Integer, b As Integer, counter As Integer
Dim z As String, y As String, char1 As String, str1 As String
z = 0
For x = 1 To Len(Text1.Text)
y = Mid(Text1.Text, x, 1)
If (y) = "ng" Then
z = z + 1
Label.Caption = z
End If
Next
counter = 0
str1 = Text1.Text
For b = i To Len(str1)
char1 = char1 + Replace(str1, char1, "", i)
Form1.Caption = Form1.Caption + char1
Next
End Sub
-----------
this code should display a counter for every occurence of the "ng" but i do not know if what part of this code does not work... help pls... thanx
Re: newbie here... need help on built-in functions...
Sorry, I'm a bit confused! At the moment your code displays the contents of text1 in the form caption is that what you want?
Re: newbie here... need help on built-in functions...
If you want to count the number of "ng"s in a string just do this.
Code:
Const TEST_STRING = "testing testing testing"
Const LOOK_FOR = "ng"
MsgBox (Len(TEST_STRING) - Len(Replace(TEST_STRING, LOOK_FOR, ""))) / Len(LOOK_FOR)
Re: newbie here... need help on built-in functions...
Quote:
Originally Posted by
Nightwalker83
Sorry, I'm a bit confused! At the moment your code displays the contents of text1 in the form caption is that what you want?
No sir, the text1 will only get input from the user... what i am trying to make is that i only want to count how many "ng" the program encounters and displays how many "ng" occurs..
example
Re: newbie here... need help on built-in functions...
Martin's example is very neat.
If you wanted to use the Len and Mid$ Functions then here's another way of doing it
Code:
Option Explicit
Private Sub Command_Click()
Dim intI As Integer
Dim intNg As Integer
Dim strCheck As String
intI = 1
Do
'
' Pick up the intI'th and intI'th + 1 characters
' and assign to strCheck
'
strCheck = Mid$(Text1.Text, intI, 2)
'
' are they 'ng' ?
'
If strCheck = "ng" Then
'
' Yes then add 1 to the count
'
intNg = intNg + 1
End If
'
' Move to the next character position
' by adding 1 to intI
'
intI = intI + 1
'
' Loop until all characters have been checked
'
Loop Until intI = Len(Text1.Text)
MsgBox "There were " & intNg & " occurences of 'ng' found in Text1.Text"
End Sub
Re: newbie here... need help on built-in functions...
FYI: One of the reasons your code fails is this line: y = Mid(Text1.Text, x, 1)
How can y = "ng" if the Mid() function is only returning 1 character?
Re: newbie here... need help on built-in functions...
Quote:
Originally Posted by
LaVolpe
FYI: One of the reasons your code fails is this line: y = Mid(Text1.Text, x, 1)
How can y = "ng" if the Mid() function is only returning 1 character?
and if he fixes that he will have to change line one also to len(text1.text)-1
Re: newbie here... need help on built-in functions...
Thanx for the help everyone... i have found what i wanted to do...