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

org.wiztools.commons.StringUtil Maven / Gradle / Ivy

There is a newer version: 0.4.1
Show newest version
/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */
package org.wiztools.commons;

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

/**
 *
 * @author subwiz
 */
public final class StringUtil {

    // Don't allow initialization of this class:
    private StringUtil(){}

    /**
     * Checks if the String is null, or an empty string.
     * @param str The string to check.
     * @return Return value of the validation.
     */
    public static boolean isEmpty(final String str) {
        if (str == null || str.trim().isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * Checks if the string is not null and not empty.
     * @param str
     * @return true is not empty.
     */
    public static boolean isNotEmpty(final String str) {
        return !isEmpty(str);
    }

    /**
     * 
     * @param str The string to verify for null content.
     * @return Returns null string if null is encountered, else the same reference to the string.
     */
    public static String getNullStrIfNull(final String str) {
        return str == null ? "" : str;
    }

    /**
     * A method similar to PHP's implode() function (http://php.net/implode)
     * @param glue The String to glue pieces together.
     * @param pieces An array of String which needs to be glued.
     * @return Returns the concatenated string.
     */
    public static String implode(final String glue, final String[] pieces){
        StringBuilder sb = new StringBuilder();
        final int last = pieces.length;
        int count = 1;
        for(String str: pieces){
            sb.append(str);
            if(count pieces){
        return implode(glue, pieces.toArray(STRING_ARRAY));
    }

    /**
     * A method similar to PHP's implode() function (http://php.net/implode)
     * @param pieces A collection of String which needs to be glued.
     * @return Returns the concatenated string.
     */
    public static String implode(final Collection pieces){
        return implode(pieces.toArray(STRING_ARRAY));
    }

    /**
     * A method similar to PHP's explode() function (http://php.net/explode)
     * This method does not use RegularExpression based split as in Java. This
     * makes this method much faster than Java's implementation.
     * @param delimiter The delimiter string to split the string with.
     * @param str The input string.
     * @return The List of split string.
     */
    public static List explode(final String delimiter, final String str) {
        final List out = new ArrayList();
        final int len = delimiter.length();

        int startIndex = 0;
        int currIndex = 0;
        while((currIndex = str.indexOf(delimiter, startIndex)) != -1) {
            final String sub = str.substring(startIndex, currIndex);
            out.add(sub);
            startIndex = currIndex + len;
        }
        // Get the tail end of the split string
        final String sub = str.substring(startIndex);
        out.add(sub);

        return out;
    }

    /**
     * When delimiter is found in the string, the string is split into two parts:
     * sub-string till the delimiter, and the string after the delimiter.
     * @param delimiter The delimiter string.
     * @param str The string to operate on.
     * @return List of the result.
     */
    public static List explodeFirst(final String delimiter, final String str) {
        final List out = new ArrayList(2);

        final int beginIndex = str.indexOf(delimiter);
        if(beginIndex != -1) {
            final String subFirst = str.substring(0, beginIndex);
            final String subLast = str.substring(beginIndex + delimiter.length());

            out.add(subFirst);
            out.add(subLast);
        }

        return out;
    }

    /**
     * Splits the string into two parts based on the last occurrence of the delimiter.
     * @param delimiter The delimiter.
     * @param str The string to operate on.
     * @return The List of exploded string.
     */
    public static List explodeLast(final String delimiter, final String str) {
        final List out = new ArrayList(2);

        final int lastIndex = str.lastIndexOf(delimiter);
        if(lastIndex != -1) {
            final String subFirst = str.substring(0, lastIndex);
            final String subLast = str.substring(lastIndex + delimiter.length());

            out.add(subFirst);
            out.add(subLast);
        }

        return out;
    }

    /**
     * Capatilize only the first letter of the string.
     * @param str The input string.
     * @return The capatilized string.
     */
    public static String capatilizeFirstLetter(final String str) {
        final char[] c = str.toCharArray();
        if(c.length > 0) {
            c[0] = Character.toUpperCase(c[0]);
        }
        return String.valueOf(c);
    }

    /**
     * Capatilize first letter of each word after a space, dot (.) or single-quote (').
     * Usually used for converting people names to human-addressable formats.
     * @param str The input string.
     * @return The final capatilized string.
     */
    public static String capatilizeFirstLetterEachWord(final String str) {
        char[] c = str.toLowerCase().toCharArray();
        boolean found = false;
        for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy