Posts

Showing posts with the label String Truncation

A New Collection of Thoughtful Learning Apps — Now Available on iOS & Android

Image
I’m excited to share a set of mobile apps I’ve recently completed and published on both the Google Play Store and the Apple App Store. These apps are designed with a simple goal in mind: to make meaningful, structured content more accessible, whether you’re studying theology or improving your English vocabulary. 📱 Now Available on Both Platforms All apps are live and available for download: Google Play Developer Page: https://play.google.com/store/apps/dev?id=5835943159853189043 Apple App Store Developer Page: https://apps.apple.com/ca/developer/q-z-l-corp/id1888794100 📖 Theology & Confession Study Apps For those interested in Reformed theology and classical Christian teachings, I’ve developed a series of apps that present foundational texts in a clean, focused reading format: The Belgic Confession Canons of Dort Heidelberg Catechism Westminster Shorter Catechism Each app is designed to provide a distraction-free experience, making it easier to read, reflect, and revisit these im...

How to Safely Truncate a String by Byte Length in Java (UTF-8 Solution)

How to Safely Truncate a String by Byte Length in Java (UTF-8 Solution) In Java backend development, it’s common to enforce size limits on strings, such as: Database column byte limits API payload size constraints Logging or message size restrictions A typical approach is: value.substring(0, n); However, this is not safe when dealing with UTF-8 encoding . The Core Problem: Characters ≠ Bytes In UTF-8 encoding: ASCII characters → 1 byte Chinese characters → 3 bytes Emoji → 4 bytes For example: "hello" → 5 bytes "你好" → 6 bytes "😊" → 4 bytes This means: Truncating by character count does NOT guarantee byte size limits --- Does Java Provide a Built-in Solution? No — Java does not provide a direct method to safely truncate a string by byte length. While Java offers low-level APIs such as: CharsetEncoder ByteBuffer String.getBytes() They are not designed for simple, safe truncation and requ...

Fix “Value Too Long for Column” Error: UTF-8 String Truncation by Bytes in Java

Image
Fix “Value Too Long for Column” Error: UTF-8 String Truncation by Bytes in Java If you've ever encountered the "value too long for column" database error, especially in a Java backend, the root cause may not be obvious. In this post, we’ll walk through a real production issue caused by UTF-8 encoding , explain why string length is not equal to byte length , and show how to correctly truncate strings by bytes without breaking multibyte characters. The Production Issue We hit a production error during a database insert: value too long for column Database column limit: 50 bytes Input string length: 53 characters Code already truncated to 50 characters At first glance, everything looked correct—but the insert still failed. Root Cause: UTF-8 Encoding The issue comes down to this: 50 characters does NOT equal 50 bytes in UTF-8 In UTF-8 encoding: ASCII characters → 1 byte Chinese characters → 3 bytes Emoji → 4 bytes This mean...