Skip to content

Latest commit

 

History

History
18 lines (15 loc) · 486 Bytes

truncate-string.md

File metadata and controls

18 lines (15 loc) · 486 Bytes
title description author tags
Truncate String
Truncates a string after a specified length (can also be used for hiding information)
Mcbencrafter
string,truncate,mask,hide
public static String truncate(String text, int length, String suffix) {
    if (text.length() <= length)
        return text;
    
    return text.substring(0, length).trim() + (suffix != null ? suffix : "");
}

// Usage:
System.out.println(truncate("hello world", 5, "...")); // "hello..."