attipa
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?
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?
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
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.RegularExpress ions.Regex (patternWo rds)
Return regCountWords.Matches(inpu tText).Cou nt()
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
Public Function CountWords(ByVal inputText As String) As Integer
Dim patternWords As String = "[\w]+"
Dim regCountWords As New System.Text.RegularExpress
Return regCountWords.Matches(inpu
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
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)
Dim r As String = reader("Text")
r = r.Substring(40)
That 40 words, not characters.
Bob
Bob
Oops. My bad.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Bob;
I would like to see a resolution to this question which I gave a working solution.
Thanks;
Fernando
I would like to see a resolution to this question which I gave a working solution.
Thanks;
Fernando
text = text.Replace("<br>","").To
text = text.Replace("</b>","").To