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?
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()
0
 
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
0
 
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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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)
0
 
Bob LearnedCommented:
That 40 words, not characters.

Bob
0
 
JRossi1Commented:
Oops. My bad.
0
 
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
0

Experts Exchange Solution brought to you by ConnectWise

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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.