All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.ericdriggs.reportcard.util.truncate.TruncateUtils Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
package io.github.ericdriggs.reportcard.util.truncate;

import java.nio.charset.StandardCharsets;

public enum TruncateUtils {
    ;//static methods only

    public static String truncateBytes(String str, int maxStringBytes) {

        if (str == null) {
            return null;
        }

        //Nothing to do
        final byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        if (bytes.length <= maxStringBytes) {
            return str;
        }

        //Iteratively trim until below target length
        for (int i = maxStringBytes; i > 0; i--) {
            final String subString = str.substring(0, i);
            final byte[] subStringBytes = subString.getBytes(StandardCharsets.UTF_8);
            if (subStringBytes.length <= maxStringBytes) {
                return subString;
            }
        }
        throw new IllegalStateException("truncation coding error -- should be unreachable code");
    }

    public static String truncateString(String str, int maxLength) {
        if (str == null) {
            return null;
        }
        if (str.length() <= maxLength) {
            return str;
        }
        return str.substring(0, maxLength);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy