{# canonical_base is the OWNING tenant's origin: all 16 Peasy domains serve the same catalogue, so a page rendered by a non-owner points its canonical at the owner instead of competing with it. Falls back to this site for static/self-owned pages. #}
🍋
Menu
Best Practice Beginner 1 min read 262 words

String Manipulation Best Practices for Developers

Best practices for common string operations — trimming, padding, case conversion, truncation, and escaping. Covers Unicode-aware handling and the subtle bugs caused by naive string processing.

Key Takeaways

  • Many string operations that seem simple with ASCII become complex with Unicode.
  • Unicode defines four normalization forms.
  • Different contexts require different escaping:
  • Use grapheme-cluster-aware operations when dealing with user-visible text.
  • Always normalize before comparison, hashing, or storage to prevent phantom duplicates.

Unicode-Aware String Processing

Many string operations that seem simple with ASCII become complex with Unicode. A 'character' can be multiple code points (emoji with skin tone modifiers), a 'word' boundary differs by language, and 'uppercase' transforms are locale-dependent. Using the correct abstraction level prevents subtle data corruption.

Common Operations and Pitfalls

Operation ASCII Safe Unicode Pitfall
Length str.length Emoji 👨‍👩‍👧 is 5 code points, 1 grapheme
Uppercase toUpperCase() Turkish iİ (not I)
Reverse Swap chars Combining marks detach from base characters
Truncate Slice at index May split surrogate pairs or grapheme clusters
Compare === é (one codepoint) ≠ é (e + combining accent)

Normalization

Unicode defines four normalization forms. NFC (Canonical Decomposition + Composition) is the web standard — it composes characters into their precomposed forms where possible. Always normalize before comparison, hashing, or storage to prevent phantom duplicates.

Escaping and Encoding

Different contexts require different escaping:

  • HTML: &, <, >, ", ' → entities
  • URL: Non-ASCII and reserved chars → percent encoding
  • JSON: Backslash, quotes, control characters → backslash escape
  • SQL: Use parameterized queries (never manual escaping)
  • Regex: \, ., *, +, ?, (, ), [, { → backslash

Best Practices

Use grapheme-cluster-aware operations when dealing with user-visible text. Use code-point-level operations for protocol and storage layer work. Process text with the Peasy string tools for Unicode-safe operations.