CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    103

    Question Count # repeats of string within string

    Anyone know of a nifty VB command that will tell you the number or times a string appears within another string?

    For example, let's say we have a string that will be placed in a txtBox where .multiline=True. It'd be nice to know how many CrLfs are in the string so you can determine the # of lines to be displayed.

    It's easy enough to write code to do this, but maybe VB does it for me. Thanks...

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Count # repeats of string within string

    Yes there is this very clever line out of Davids Catskin-program:

    n = Len(Text1.Text) -Len(Replace(Text1.Text, vbLF, ""))

    n is the number of vbLf chars in the Text1.Text, but you will watch out: if the last line has no CrLf at the end it is not the actual number of lines.

    You could also get the number of lines by using the SendMessage() API
    Code:
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
           ByVal hWnd As Long, ByVal wMsg As Long, _
           ByVal wParam As Long, lParam As Long _
      ) As Long
    
    Private Const EM_GETLINECOUNT = &HBA
    
    n = SendMessage(Text1.hWnd, EM_GETLINECOUNT, 0, 0)

  3. #3
    Join Date
    Apr 2005
    Posts
    103

    Thumbs up Re: Count # repeats of string within string

    Excellent! How obvious! Thanks very much!

  4. #4
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Count # repeats of string within string

    This also works:
    Code:
    Private Function NumberInString(sSource As String, sMatch As String) As Long
    
        Dim sTemp() As String
        
        sTemp = Split(sSource, sMatch)
        NumberInString = UBound(sTemp)
    
    End Function
    Calling code example:
    Code:
    Debug.Print NumberInString("abcabcabc", "bc")
    Resist the temptation to single line the call, ie:
    Code:
    Debug.Print UBound(Split(sSource, sMatch))
    Passing the return array of a function directly to UBound is a known bug in VB6 and will leak memory. Note that this method of WoF's...
    Code:
    n = Len(Text1.Text) -Len(Replace(Text1.Text, vbLF, ""))
    ...only works for single characters.

    @WoF -- Nice call on the API. Didn't know you could do that.
    Last edited by Comintern; January 9th, 2008 at 06:23 PM. Reason: Add note.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Count # repeats of string within string

    You can expand the method I used also to multiple characters. You only have to divide the result by the len of the searchstring.
    Code:
    n = (Len(Text1.Text) -Len(Replace(Text1.Text, StringToCount, ""))) / Len(StringToCount)
    I'd like to point out again that both, this method and your NumberInString() work perfectly for the purpose of counting characters, but will also trip when used for counting lines by the presence of CrLf. If the last line is not terminated with a CrLf, as it is often, it will not count. So the linecount could be incorrect. SendMessage() gives the real linecount.

    Yes, I found the SendMessage() a sheer bottmless resource for functions I always wanted, like the LB_SELECTSTRING and much more.
    I wonder if there is ever a complete documentation which wraps everything up. It's always so much trouble to find things out. You try and think up complex solutions and then you find out, there is a SendMessage constant which does the trick.
    Last edited by WoF; January 10th, 2008 at 08:32 AM.

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

Featured