Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to find substring count occurs in a string in asp.net

how to find substring count occurs in a string in asp.net

Scheduled Pinned Locked Moved ASP.NET
csharptutorialasp-nethelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    unil Dhiman
    wrote on last edited by
    #1

    HI All can anybody tell me how to find substring count occurs in a string in asp.net C#. for example.. string str = "Ram was not there. Ram has been gone to market"; if I wand to find count of "Ram" in this string , then it should return 2 please help me.. thanks

    E C 2 Replies Last reply
    0
    • U unil Dhiman

      HI All can anybody tell me how to find substring count occurs in a string in asp.net C#. for example.. string str = "Ram was not there. Ram has been gone to market"; if I wand to find count of "Ram" in this string , then it should return 2 please help me.. thanks

      E Offline
      E Offline
      eyeseetee
      wrote on last edited by
      #2

      //Function to count no.of occurences of Substring in Main string public static int CharCount(String strSource,String strToCount) { int iCount=0; int iPos=strSource.IndexOf(strToCount); while(iPos!=-1) { iCount++; strSource=strSource.Substring(iPos+1); iPos=strSource.IndexOf(strToCount); } return iCount; }

      U 1 Reply Last reply
      0
      • U unil Dhiman

        HI All can anybody tell me how to find substring count occurs in a string in asp.net C#. for example.. string str = "Ram was not there. Ram has been gone to market"; if I wand to find count of "Ram" in this string , then it should return 2 please help me.. thanks

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        This is a C# question. The easiest way probably is to call the split method and see how many elements it returns.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        1 Reply Last reply
        0
        • E eyeseetee

          //Function to count no.of occurences of Substring in Main string public static int CharCount(String strSource,String strToCount) { int iCount=0; int iPos=strSource.IndexOf(strToCount); while(iPos!=-1) { iCount++; strSource=strSource.Substring(iPos+1); iPos=strSource.IndexOf(strToCount); } return iCount; }

          U Offline
          U Offline
          unil Dhiman
          wrote on last edited by
          #4

          Thanks eyeseetee.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups