Link to home
Start Free TrialLog in
Avatar of attipa
attipaFlag for United States of America

asked on

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?
Avatar of Mikal613
Mikal613
Flag of United States of America image

text  = text.Replace("<b>","").ToString()
text  = text.Replace("<br>","").ToString()
text  = text.Replace("</b>","").ToString()
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
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
Avatar of JRossi1
JRossi1

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

Bob
Oops. My bad.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
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
Hi Bob;

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

Thanks;

Fernando