deleting characters out of a long string

Posted on 2006-04-06
Last Modified: 2010-04-23
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?
Question by:attipa
    LVL 48

    Expert Comment

    text  = text.Replace("<b>","").ToString()
    text  = text.Replace("<br>","").ToString()
    text  = text.Replace("</b>","").ToString()
    LVL 48

    Expert Comment

    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)
        exit while
      end if
    end while
    return i
    end function
    LVL 96

    Expert Comment

    by:Bob Learned
    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

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

    LVL 6

    Expert Comment

    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)
    LVL 96

    Expert Comment

    by:Bob Learned
    That 40 words, not characters.

    LVL 6

    Expert Comment

    Oops. My bad.
    LVL 62

    Accepted Solution

    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

    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi Bob;

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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now