how to find substring count occurs in a string in asp.net
-
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
-
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
//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; }
-
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
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 )
-
//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; }
Thanks eyeseetee.