Results 1 to 8 of 8

Thread: [RESOLVED] newbie here... need help on built-in functions...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    19

    Resolved [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

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    19

    Re: newbie here... need help on built-in functions...

    Quote Originally Posted by Nightwalker83 View Post
    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

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: newbie here... need help on built-in functions...

    Quote Originally Posted by LaVolpe View Post
    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
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    19

    Re: newbie here... need help on built-in functions...

    Thanx for the help everyone... i have found what i wanted to do...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width