?
Solved

deleting characters out of a long string

Posted on 2006-04-06
10
Medium Priority
?
244 Views
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?
0
Comment
Question by:attipa
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 16395091
text  = text.Replace("<b>","").ToString()
text  = text.Replace("<br>","").ToString()
text  = text.Replace("</b>","").ToString()
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16395119
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16395132
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:JRossi1
ID: 16395239
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16395252
That 40 words, not characters.

Bob
0
 
LVL 6

Expert Comment

by:JRossi1
ID: 16395286
Oops. My bad.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16395553
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16605083
Hi Bob;

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

Thanks;

Fernando
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 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