Java String Format format(String string)

Here you can find the source of format(String string)

Description

format String , for example: 1,2,3,4,6,8 ====> 1-4,6,8.

License

Open Source License

Parameter

Parameter Description
string a parameter

Declaration

public static String format(String string) 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**/*w w  w. j a  v  a 2 s  . co m*/
     * format String , for example: 1,2,3,4,6,8 ====> 1-4,6,8.
     * 
     * @param string
     * @return
     */
    public static String format(String string) {
        if (string == null || string.trim().length() == 0 || string.equals("*")) { //$NON-NLS-1$
            return string;
        }

        String delimiter = ","; //$NON-NLS-1$

        List<StringBuffer> sections = new ArrayList<StringBuffer>();
        String[] days = string.split(delimiter);
        String tempDay = null;
        for (int i = 0; i < days.length; i++) {
            String day = days[i];
            if (tempDay != null && isConsecutiveDays(tempDay, day)) {
                StringBuffer tempSb = getLastSectionDays(sections);
                tempSb.append(delimiter);
                tempSb.append(day);
            } else {
                StringBuffer sb = new StringBuffer();
                sb.append(day);
                sections.add(sb);
            }
            tempDay = day;
        }

        StringBuffer sb = new StringBuffer();
        for (StringBuffer section : sections) {
            String[] sectionDays = section.toString().split(delimiter);
            if (sectionDays.length > 2) {
                sb.append(sectionDays[0]);
                sb.append("-"); //$NON-NLS-1$
                sb.append(sectionDays[sectionDays.length - 1]);
            } else {
                sb.append(section);
            }
            sb.append(delimiter);
        }

        if (sb.length() > 1) {
            return sb.substring(0, sb.length() - 1);
        }

        return sb.toString();
    }

    /**
     * check if two days are consecutive.
     * 
     * @param tempDay
     * @param day
     * @return
     */
    private static boolean isConsecutiveDays(String tempDay, String day) {
        if (Integer.valueOf(day) - Integer.valueOf(tempDay) == 1) {
            return true;
        }
        return false;
    }

    private static StringBuffer getLastSectionDays(List sections) {
        if (sections.size() == 0) {
            return new StringBuffer();
        } else {
            return (StringBuffer) sections.get(sections.size() - 1);
        }
    }
}

Related

  1. format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)
  2. format(String name, char separator)
  3. format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)
  4. format(String s)
  5. format(String source, String placeholder, Object... objs)
  6. formatCommand(String cmd)
  7. formatDescriptors(String descriptorString)
  8. formatError(String errorMsg)
  9. formatJson(String json)