Results 1 to 7 of 7

Thread: how many times a string appears in another string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    17

    how many times a string appears in another string

    codes like(x is variable)


    dim k as string="5b5" & x & "85"

    I want to know how many times "5" appears in k,thanks
    Last edited by vbsunst; Jun 4th, 2009 at 02:00 AM.

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: how many times a string appears in another string

    Quote Originally Posted by vbsunst View Post
    codes like(x is variable)


    dim k as string="5b5" & x & "85"

    I want to know how many times "5" appears in k,thanks
    try this. i know my way is not best way. but you can refer it.

    Code:
      Dim a As Integer = 0
            Dim k As String = "5b585"
            For i As Integer = 0 To Len(k) - 1
                If k.Substring(i, 1).Contains("5") = True Then
                    a = a + 1
                End If
            Next
            MsgBox("numbers of 5 is : " & a, MsgBoxStyle.Information, "info")
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  3. #3
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: how many times a string appears in another string

    hey vbsunst
    you may use following
    Code:
    Dim sText As String = "5b585"
    		Dim cMatch as String = "5"
    		Dim nCount as Integer
    		For Each sT As String In  sText.ToCharArray()
    			If sT = cMatch Then
    				nCount=nCount+1		
    			End If
    		Next
    		MessageBox.Show(nCount)
    Last edited by su ki; Jun 4th, 2009 at 03:47 AM.
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: how many times a string appears in another string

    I would go for regular expressions

    Code:
     Dim regx As New System.Text.RegularExpressions.Regex("5")
            Dim str As String = "1251werwer2225555"
            Dim Count As Integer = 0
            If regx.IsMatch(str) Then
                Count = regx.Matches(str).Count
            End If
            MessageBox.Show(Count)
    Please mark you thread resolved using the Thread Tools as shown

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    21

    Re: how many times a string appears in another string

    Code:
    Public Function StringCount(ByVal search As String, ByVal find As String) As Integer
        If String.IsNullOrEmpty(search) OrElse String.IsNullOrEmpty(find) Then
            Return 0
        End If
    
        Dim index As Integer = -1
        Dim count As Integer = -1
        Do
            index = search.IndexOf(find, index + 1, StringComparison.CurrentCultureIgnoreCase)
            count += 1
        Loop While index > -1
        Return count
    End Function

  6. #6
    Junior Member
    Join Date
    May 2009
    Posts
    21

    Re: how many times a string appears in another string

    Oops I accidently posted this twice. Anyway this is just a function that counts the occurrences of a string using the built-in IndexOf and only searches after the last occurrence of the string so it should be pretty efficient. Also it ignores case using the current culture. You could easily add the culture as another parameter.

    Edit:
    Additionally, this will work for searching whole strings and not just a single character in a string. The functions above look like they will only work for a single character.

    Example usage:
    Dim count As Integer = StringCount("5b585", "5")

    Code:
    Public Function StringCount(ByVal search As String, ByVal find As String) As Integer
        If String.IsNullOrEmpty(search) OrElse String.IsNullOrEmpty(find) Then
            Return 0
        End If
    
        Dim index As Integer = -1
        Dim count As Integer = -1
        Do
            index = search.IndexOf(find, index + 1, StringComparison.CurrentCultureIgnoreCase)
            count += 1
        Loop While index > -1
        Return count
    End Function
    Last edited by insomnicoder; Jun 4th, 2009 at 12:06 PM. Reason: Duplicate

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: how many times a string appears in another string

    You can use LINQ for this to do a count, no need for loops.

    Code:
            Dim MatchingCharCount = Aggregate C As Char In k _
                                    Where C = "5" _
                                    Into Count()
    
            MessageBox.Show("5 was found: " & MatchingCharCount & " times")
    EDIT: sorry I did not see you were using VS2005. LINQ is in VS2008.

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