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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.