Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Generalize integer number with specific separator and segment length.
     * @param number
     * @param separator Eg. "," or "." ...
     * @param segmentLenght
     * @return
     */
    public static String generalizeIntegerNumber(long number, String separator, int segmentLenght) {
        return generalizeIntegerNumber(String.valueOf(number), separator, segmentLenght);
    }

    /**
     * Generalize string number with specific separator and segment length.
     * @param numberString
     * @param separator Eg. "," or "." ...
     * @param segmentLenght
     * @return
     */
    public static String generalizeIntegerNumber(String numberString, String separator, int segmentLenght) {
        String genString = "";
        if (numberString != null) {
            for (int i = numberString.length() - 1; i >= 0; i -= segmentLenght) {
                if (i - segmentLenght >= 0) {
                    genString = numberString.substring(i - segmentLenght + 1, i + 1)
                            + (genString.isEmpty() ? "" : separator + genString);
                } else {
                    genString = numberString.substring(0, i + 1)
                            + (genString.isEmpty() ? "" : separator + genString);
                }
            }
        }
        return genString;
    }

    /**
     * Check a string is whether empty or not.
     * @param s The string need to check empty.
     * @return [true] if string is empty or null. Otherwise is [false].
     */
    public static boolean isEmpty(String s) {
        if (s != null) {
            s = s.trim();
        }
        return (s == null || "".equals(s));
    }
}