Remove HTML TagsUse a for-range loop to iterate over the runes in a string and remove html markup from the result.
This page was last reviewed on Aug 25, 2023.
Remove HTML. Suppose a string in a Golang program contains HTML markup, but we do not want to keep the markup. We can remove the tags with a for-loop.
With a rune slice, we can build up the runes for the result. We detect markup by testing each rune in the original string for angle brackets.
Example. We introduce the stripHtml function, which receives a string and returns another string. We call stripHtml to test in the main() function.
Step 1 We loop over the input string with a for-range loop. This gives us each individual rune in the string.
Step 2 We append each rune to the data rune slice. At this point, we have skipped past runes including and surrounded by angle brackets.
Step 3 We convert the rune slice back into a string. This string now contains all non-markup runes.
Convert String, Rune Slice
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.
This page was last updated on Aug 25, 2023 (new).
© 2007-2023 Sam Allen.