Java Utililty Methods String Format

List of utility methods to do String Format

Description

The list of methods to do String Format are organized into topic(s).

Method

FormattercreateFormatter()
create Formatter
return new Formatter() {
    final MessageFormat messageFormat = new MessageFormat("{0}{1,date,HH:mm:ss} {2} - {3}{4}\n");
    @Override
    public synchronized String format(LogRecord record) {
        Object[] arguments = new Object[] { String.format("%-8s", record.getLevel()),
                new Date(record.getMillis()),
                record.getLoggerName() == null ? "<Unknown Logger>"
                        : record.getLoggerName().substring(record.getLoggerName().lastIndexOf('.') + 1),
...
Stringformat(final String message, final Object... args)
format
try {
    for (int i = 0; i < args.length; i++) {
        final Object o = args[i];
        if (o != null && o.getClass().isArray()) {
            args[i] = Arrays.asList((Object[]) o);
    return String.format(message, args);
...
Stringformat(final String s, final int width, final int intend)
format
final String ws = ws(intend);
if (s.length() < width - intend)
    return ws + s;
final StringBuilder buf = new StringBuilder();
int pos = 0;
while (pos + width - intend < s.length()) {
    buf.append(ws).append(s.substring(pos, pos + width - intend)).append("\n");
    pos += width - intend;
...
Stringformat(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)
Format long message in multiple line in such a way that each line does not contain more charPerLine .
char[] padChar = new char[paddingLeft];
longMessage = longMessage.replaceAll("<[/]*[a-zA-Z]+>", "");
Arrays.fill(padChar, ' ');
StringBuilder builder = new StringBuilder();
if (padFirstLine) {
    builder.append(padChar);
String[] parts = longMessage.split(" ");
...
Stringformat(String name, char separator)
format
StringBuffer result = new StringBuffer();
for (Iterator<String> i = parseName(name, '_').iterator(); i.hasNext();) {
    String component = i.next();
    result.append(component);
    if (i.hasNext() && component.length() > 1) {
        result.append(separator);
return result.toString();
Stringformat(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)
Formats a name by parsing it into words separated by underscores and/or mixed-casing and then recombining them using the specified separator.
String leadingSeparators = includeLeadingSeparator ? getLeadingSeparators(name, '_') : null;
if (leadingSeparators != null) {
    name = name.substring(leadingSeparators.length());
List<String> parsedName = new ArrayList<String>();
if (prefix != null && name.startsWith(prefix) && name.length() > prefix.length()
        && Character.isUpperCase(name.charAt(prefix.length()))) {
    name = name.substring(prefix.length());
...
Stringformat(String s)
format
String[] fullNameSplit = s.split("_| ");
List<String> fullNameList = new ArrayList<String>();
for (String f : fullNameSplit) {
    fullNameList.add(f);
String name = "";
for (String f : fullNameList) {
    name = name + f.replace(f.substring(1), f.substring(1).toLowerCase());
...
Stringformat(String source, String placeholder, Object... objs)
format
for (int idx = 0; idx < objs.length; idx++) {
    if (source.contains(placeholder + idx) && objs[idx] != null) {
        source = source.replace(placeholder + idx, objs[idx].toString());
return source;
Stringformat(String string)
format String , for example: 1,2,3,4,6,8 ====> 1-4,6,8.
if (string == null || string.trim().length() == 0 || string.equals("*")) { 
    return string;
String delimiter = ","; 
List<StringBuffer> sections = new ArrayList<StringBuffer>();
String[] days = string.split(delimiter);
String tempDay = null;
for (int i = 0; i < days.length; i++) {
...
StringformatCommand(String cmd)
Format the specified command line.
if (cmd == null) {
    return null;
cmd = cmd.replaceFirst(" <defunct>", "");
if (cmd.matches("\\[.+\\]")) {
    cmd = cmd.substring(1, cmd.length() - 1);
String[] tokens = cmd.trim().split("\\s+");
...