[x]
Ever Worried about Missed / Lost Mobile Phone (or) Mobile Phone Theft ?
Use GinGly to Save your Mobile Numbers and Useful Messages
Limited Time Free Access .Hurry Up !!!


Login with username, password and session length
Resend Activation Email | Forgot your Password?
Join IT Acumens Discussion Zone Free!

Share this topic on FacebookShare this topic on MySpaceShare this topic on Del.icio.usShare this topic on DiggShare this topic on RedditShare this topic on StumbleUponShare this topic on TwitterShare this topic on TechnoratiShare this topic on MagnoliaShare this topic on GoogleShare this topic on Yahoo

Author [EN] [PL] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] Topic: Count the occurrences of words in a String - String Operations  (Read 2543 times)

Offline VelMurugan

  • Soft Engg, IT Acumens
  • Moderator
  • Hero Member
  • *****
  • Posts: 11292
  • Programmer, IT Acumens
    • View Profile
    • IT Acumens
Count the occurrences of words in a String

You can adopt multiple ways to find the occurrence of a word in a string. One of them is to use the String.IndexOf() which is one of the ways of finding the occurrence of the word. In VB.NET, use String.InStr().
Another simple way is to use Count property of the Regex.Matches() collection. However this method is slow. We will explore both these methods in the sample.

C#
   
Code: [Select]
  string strOriginal = "These functions will come handy";
    string strModified = String.Empty;

// Using IndexOf
     int strt = 0;
     int cnt = -1;
     int idx = -1;
     strOriginal = "She sells sea shells on the sea shore";
     string srchString = "sea";
     while (strt != -1)
     {
         strt = strOriginal.IndexOf(srchString, idx + 1);
         cnt += 1;
         idx = strt;
     }
     MessageBox.Show(srchString + " occurs " + cnt + " times");
 
 
    // Using Regular Expression
     System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(srchString);
    int count = rex.Matches(strOriginal).Count;
    MessageBox.Show(srchString + " occurs " + count + " times");

VB.NET
   
Code: [Select]
      Dim strOriginal As String = "These functions will come handy"
    Dim strModified As String = String.Empty

 ' Using IndexOf
       Dim strt As Integer = 0
       Dim cnt As Integer = -1
       Dim idx As Integer = -1
       strOriginal = "She sells sea shells on the sea shore"
       Dim srchString As String = "sea"
       Do While strt <> -1
             strt = strOriginal.IndexOf(srchString, idx + 1)
             cnt += 1
             idx = strt
       Loop
       MessageBox.Show(srchString & " occurs " & cnt & " times")
 
 
      ' Using Regular Expression
       Dim rex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(srchString)
      Dim count As Integer = rex.Matches(strOriginal).Count
      MessageBox.Show(srchString & " occurs " & count & " times")

GoogleTagged


 

Related Topics

  Subject / Started by Replies Last post
0 Replies
678 Views
Last post August 21, 2008, 02:43:18 PM
by VelMurugan
0 Replies
188 Views
Last post August 21, 2008, 02:49:01 PM
by VelMurugan
0 Replies
1007 Views
Last post August 21, 2008, 02:51:50 PM
by VelMurugan
0 Replies
336 Views
Last post August 21, 2008, 02:54:13 PM
by VelMurugan
0 Replies
2784 Views
Last post August 21, 2008, 03:03:08 PM
by VelMurugan