Link to home
Start Free TrialLog in
Avatar of musclejack
musclejack

asked on

hi i have a string = "Hello<br>My<br>Friend<br>". how can i get the number of "<br>" from this string in asp.net?

hi i have a string = "Hello<br>My<br>Friend<br>".  how can i get the number of "<br>" from this string in asp.net?
Avatar of vladimir_kalashnikov
vladimir_kalashnikov

here ya go.

string brString = "Hello<br>My<br>Friend<br>"

// Count holds the number of <br>s found
int count = 0;

// Loops until it can't find any instance of <br> in the string
while(true)
{
        // finds the location of the first <br> in the string
      int i = brString.IndexOf("<br>");
      if(i < 0)
      {
            break;
      }

        // Gets rid of the string up to and including the <br>
      brString = brString.Substring(i+4);
      count++;
}

So now count represents the number of <br>s found.  If you don't want to lose the string, just put the value into a temporary string.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let's try that again:

Const BR As String = "<br>"
Dim MyString As String = "Hello<br>My<br>Friend<br>"
Dim NumberOfBRs As Integer = (MyString.Length - MyString.Replace(BR, String.Empty).Length) / BR.Length
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ignore my comment acperkins is correct.
acperkins :
i am sorry i calculated it wrong:(