Java String Byte string truncation Supports garbled characters

In Java, String needs to be truncated by the number of bytes if it is an API linkage with a byte limit. In Japanese, Charaset is used for 2 bytes, 3 bytes, and so on. I thought about it for a moment, so I wrote the code as a memorandum I haven't moved it, but maybe it's okay ...

Sample.java


public static String truncateByte(String s, int length, Charset charset) {
    if (StringUtils.isEmpty(s) || length <= 0) {
    	return s == null ? null : "";
    }
    if (s.getBytes(charset).length <= length) {
    	return s;
    }
    String r = new String(s.getBytes(charset), 0, length, charset);
    while (r.length() >= 0 && !s.startsWith(r)) {
    	r = r.substring(0, r.length() - 1);
    }
    return r;
}
  1. The first empty pattern is repelled
  2. Keep within the number of bytes
  3. Get the character truncated by the number of bytes
  4. Truncate the truncated characters character by character until the original character starts with the truncated character
  5. Returns the cut character

Recommended Posts

Java String Byte string truncation Supports garbled characters
Java string
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
[Java] String padding
Java string processing
Split string (Java)
Umlaut garbled characters
[Java] String comparison and && and ||
Java string multiple replacement
[Note] Java: String search
[Note] Java: String survey
About Java String class
Convert a Java byte array to a string in hexadecimal notation
[Java] How to convert a character string from String type to byte type
Zip compression with Java in Windows environment without garbled characters