Home
Swift
String Remove HTML Tags
This page was last reviewed on Aug 24, 2023.
Dot Net Perls
Remove HTML tags. In developing with the Swift 5.8 language, we may need to eliminate HTML markup from a string to enable further processing. This can be done with an iterative loop.
By detecting when the angle brackets are opened and closed, we can detect markup tags. This allows us to write a reliable markup-removing function.
Example. Consider this example and the stripHtml function. The function receives a String, and returns a new String containing all data except the HTML tags.
Step 1 We loop over the characters in the input String. In newer versions of Swift, this can be done directly.
String
for
Step 2 At this point, all the Characters that are not within tags are in the Character array. We convert this to a String and return it.
func stripHtml(source: String) -> String { var data = [Character]() var inside = false // Step 1: loop over string, and append chars not inside markup tags starting and ending with brackets. for c in source { if c == "<" { inside = true continue } if c == ">" { inside = false continue } if !inside { data.append(c) } } // Step 2: return new string. return String(data) } // Use the strip html function on this string. let input = "<p>Hello <b>world</b>!</p>" let result = stripHtml(source: input) print(input) print(result)
<p>Hello <b>world</b>!</p> Hello world!
Results. The simple parser we created adequately removes the HTML markup from our Swift string. Note that this approach can fail with comments containing HTML tags.
And Another specialized parser for HTML comments could be written if this functionality is needed.
Summary. Swift provides string-processing abilities like looping over Characters that can be used to remove HTML tags. Other similar functions can convert strings—for example, the ROT13 cipher.
ROT13
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 24, 2023 (new).
Home
Changes
© 2007-2024 Sam Allen.