Try it yourself with our free Html Entity Encoder tool — runs entirely in your browser, no signup needed.

How to HTML encode in Swift

How to HTML encode in Swift

HTML encoding is the process of converting special characters in a string into their corresponding HTML entities. This is a crucial step when displaying user-generated content on a web page or when storing data in a database to prevent security vulnerabilities like cross-site scripting (XSS). In Swift, HTML encoding can be achieved using the String class and some clever string manipulation. In this article, we will explore how to HTML encode a string in Swift, covering the most common use case, edge cases, common mistakes, and performance tips.

Quick Example

import Foundation

func htmlEncode(_ input: String) -> String {
    let encodedString = input.replacingOccurrences(of: "&", with: "&")
        .replacingOccurrences(of: "<", with: "&lt;")
        .replacingOccurrences(of: ">", with: "&gt;")
        .replacingOccurrences(of: "\"", with: "&quot;")
        .replacingOccurrences(of: "'", with: "&#x27;")
    return encodedString
}

let input = "Hello, <b>world</b>!"
let encoded = htmlEncode(input)
print(encoded) // Output: Hello, &lt;b&gt;world&lt;/b&gt;!

This example uses the replacingOccurrences(of:with:) method to replace special characters with their corresponding HTML entities.

Step-by-Step Breakdown

  1. import Foundation: We import the Foundation framework, which provides the String class and its methods.
  2. func htmlEncode(_ input: String) -> String { ... }: We define a function htmlEncode that takes a String input and returns an encoded String.
  3. let encodedString = input.replacingOccurrences(of: "&", with: "&amp;"): We start by replacing ampersands (&) with their HTML entity (&amp;).
  4. .replacingOccurrences(of: "<", with: "&lt;"): We replace less-than symbols (<) with their HTML entity (&lt;).
  5. .replacingOccurrences(of: ">", with: "&gt;"): We replace greater-than symbols (>) with their HTML entity (&gt;).
  6. .replacingOccurrences(of: "\"", with: "&quot;"): We replace double quotes (") with their HTML entity (&quot;).
  7. .replacingOccurrences(of: "'", with: "&#x27;"): We replace single quotes (') with their HTML entity (&#x27;).
  8. return encodedString: Finally, we return the encoded string.

Handling Edge Cases

Empty/null input

let input: String? = nil
if let unwrappedInput = input {
    let encoded = htmlEncode(unwrappedInput)
    print(encoded)
} else {
    print("Input is nil")
}

In this example, we use optional binding to safely unwrap the input string. If the input is nil, we print a message indicating that the input is null.

Invalid input

let input = "Hello, world!" as NSString
let encoded = htmlEncode(input as String)
print(encoded)

In this example, we cast the input string to an NSString and then back to a String. This is an invalid input scenario, but our htmlEncode function still works correctly.

Large input

let largeInput = String(repeating: "Hello, world!", count: 1000)
let encoded = htmlEncode(largeInput)
print(encoded)

In this example, we create a large input string by repeating a string 1000 times. Our htmlEncode function handles this large input without issues.

Unicode/special characters

let input = "Hello, world!"
let encoded = htmlEncode(input)
print(encoded)
let decoded = input.replacingOccurrences(of: "&amp;", with: "&")
    .replacingOccurrences(of: "&lt;", with: "<")
    .replacingOccurrences(of: "&gt;", with: ">")
    .replacingOccurrences(of: "&quot;", with: "\"")
    .replacingOccurrences(of: "&#x27;", with: "'")
print(decoded)

In this example, we encode and then decode a string containing special characters. The decoded string is identical to the original input string.

Common Mistakes

1. Forgetting to handle null inputs

// WRONG
func htmlEncode(_ input: String) -> String {
    // ...
}

// CORRECT
func htmlEncode(_ input: String?) -> String? {
    if let unwrappedInput = input {
        // ...
    } else {
        return nil
    }
}

2. Not using optional binding

// WRONG
func htmlEncode(_ input: String?) -> String {
    let encodedString = input!.replacingOccurrences(of: "&", with: "&amp;")
    // ...
}

// CORRECT
func htmlEncode(_ input: String?) -> String? {
    if let unwrappedInput = input {
        let encodedString = unwrappedInput.replacingOccurrences(of: "&", with: "&amp;")
        // ...
    } else {
        return nil
    }
}

3. Not handling large inputs

// WRONG
func htmlEncode(_ input: String) -> String {
    // ...
}

// CORRECT
func htmlEncode(_ input: String) -> String {
    // ...
    let encodedString = input.replacingOccurrences(of: "&", with: "&amp;", options: .literal, range: NSRange(location: 0, length: input.utf16.count))
    // ...
}

Performance Tips

  1. Use replacingOccurrences(of:with:options:range:): This method is more efficient than replacingOccurrences(of:with:) because it allows you to specify a range of characters to replace.
  2. Use NSRange instead of Range: NSRange is more efficient than Range when working with strings.
  3. Avoid using NSString: NSString is not as efficient as String in Swift. Use String instead.

FAQ

Q: What is the difference between HTML encoding and URL encoding?

A: HTML encoding is used to convert special characters in a string into their corresponding HTML entities, while URL encoding is used to convert special characters in a URL into their corresponding URL-safe characters.

Q: Why do I need to HTML encode user-generated content?

A: HTML encoding user-generated content helps prevent security vulnerabilities like cross-site scripting (XSS).

Q: Can I use String methods instead of NSString methods?

A: Yes, in most cases, you can use String methods instead of NSString methods.

Q: How do I decode HTML-encoded strings?

A: You can decode HTML-encoded strings by replacing HTML entities with their corresponding special characters.

Q: Can I use this code in a production environment?

A: Yes, this code is suitable for use in a production environment. However, you should always test and verify the code in your specific use case.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp