package main
import (
"fmt"
)
func stripHtml(source string) string {
data := []rune{}
inside := false
// Step 1: loop over string with range loop.
for _, c := range source {
if c == '<' {
inside = true
continue
}
if c == '>' {
inside = false
continue
}
// Step 2: append chars not inside markup tags starting and ending with brackets.
if !inside {
data = append(data, c)
}
}
// Step 3: return string based on the rune slice.
return string(data)
}
func main() {
// Call the stripHtml function.
input := "<p>Hello <b>world</b>!</p>"
result := stripHtml(input)
fmt.Println(input)
fmt.Println(result)
}<p>Hello <b>world</b>!</p>
Hello world!
In the results, we can see that the "p" and "b" tags were removed from the markup. Note that this function will fail for HTML comments—a more powerful parser would be needed.
Summary. It is possible to use regular expressions to remove markup from strings, but this offers little advantage over a for-loop. And it is usually slower.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.