• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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?
0
musclejack
Asked:
musclejack
  • 2
  • 2
2 Solutions
 
vladimir_kalashnikovCommented:
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.
0
 
Anthony PerkinsCommented:
Const BR As String = "<br>"

Dim MyString As String = "Hello<br>My<br>Friend<br>"
Dim NumberOfBRs As Integer = (MyString.Length - MyString.Replace(BR, "").Length) / BR.Length
0
 
Anthony PerkinsCommented:
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
0
 
appariCommented:
Just a correction;
acperkins :
i think Dim NumberOfBRs As Integer = (MyString.Length - MyString.Replace(BR, String.Empty).Length)  is enough, no need to divide with length of BR

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


in the example MyString length is 25
after replacing <Br> with empty string length will be 22
25-22 gives 3
if we divide it with 4 (length of <br>)?
0
 
appariCommented:
ignore my comment acperkins is correct.
acperkins :
i am sorry i calculated it wrong:(

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now