deleting characters out of a long string

I am trying to build a rss feed.

Everything is okay, I just need to remove certain phrases out of the string and cut it off after a certain number of words.

Dim text As String
text = reader("Text")

I want to remove any instances of "<br>", "<b>", and "</b>".

I also want to cutoff after 40 words (not characters) and add "..." after the last word.

How can I do this?
attipaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
text  = text.Replace("<b>","").ToString()
text  = text.Replace("<br>","").ToString()
text  = text.Replace("</b>","").ToString()
Mikal613Commented:
This counts words so you can use this to see if the count is 40  and then stop the reader and add the "and"
public function word_count(byval string1 as string, byval string2 as string) as integer
' counts number of times string2 occurs in string 1
dim s as string
s = string1.toupper
dim i as integer = 0 ' count
dim j as integer
while len(s) <> 0
  j = instr(s,s1.toupper)
  if j <> 0 then
    i += 1
    s = s.substring(j+s1.length-1)
  else
    exit while
  end if
end while
return i
end function
Bob LearnedCommented:
I use this Regex function count words:

  Public Function CountWords(ByVal inputText As String) As Integer

    Dim patternWords As String = "[\w]+"

    Dim regCountWords As New System.Text.RegularExpressions.Regex(patternWords)

    Return regCountWords.Matches(inputText).Count()

  End Function   'CountWords

You could extend it to return words:

  Public Function GetWords(ByVal input As String) As String()

    Dim pattern As String = "[\w]+"

    Dim results As MatchCollection = Regex.Matches(input, pattern)

    Dim wordList(results.Count - 1) As String

    For index As Integer = 0 To results.Count - 1
      wordList(index) = results(index).Value
    Next index

    Return wordList

  End Function    'GetWords

You could also get just the matches, and check the index:

  Public Function GetWords(ByVal input As String) As MatchCollection

    Dim pattern As String = "[\w]+"

    Return Regex.Matches(input, pattern)

  End Function    'GetWords

Example:
  For Each matchCurrent As Match In GetWords(text)
    Dim index As Integer = matchCurrent.Index
    Dim word As String = matchCurrent.Value
  Next matchCurrent

Bob
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

JRossi1Commented:
The following code is used to get a part of a string. Useful for cutting off bits that aren't needed throughout the rest of the code. It is called the SubString function. It accepts the start position, and the number of characters you wish to read from the start position.

Dim r As String =  reader("Text")
r = r.Substring(40)
Bob LearnedCommented:
That 40 words, not characters.

Bob
JRossi1Commented:
Oops. My bad.
Fernando SotoRetiredCommented:
Hi attipa;

This should do it for you.

Imports System.Text.RegularExpressions

        Dim input As String = "The long String
        Dim output As String

        ' Strip off <br>, <b>, or </b> from the long string
        output = Regex.Replace(input, "<br>|<b>|</b>", "")
        ' Get the first 40 words
        output = Regex.Match(output, "(\w+\W+){0,40}").Value


Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi Bob;

I would like to see a resolution to this question which I gave a working solution.

Thanks;

Fernando
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.