StringUtil.java Source code

Java tutorial

Introduction

Here is the source code for StringUtil.java

Source

/*
 * Copyright (c) 2008-2011 Simon Ritchie.
 * All rights reserved. 
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program.  If not, see http://www.gnu.org/licenses/>.
 */
//package org.rimudb.editor.util;

public class StringUtil {

    public static final String SC_LOWER = "lower";
    public static final String SC_UPPER = "upper";
    public static final String SC_SENTENCE = "sentence";
    public static final String SC_TITLE = "title";
    public static final String SC_PROPER = "proper";

    /**
     * Private Constructor - this is a static methods only class.
     */
    private StringUtil() {
        super();
    }

    /**
     * <P>Right justify a string using blanks to pad the left part of the
     * string until it reaches <code>maxLength</code>.  If the length of
     * the string is greater than <code>maxLength</code> characters, return
     * only the first, left <code>maxLength</code> characters.</P>
     *
     */
    public static String rightJustify(String s, int maxLength) {
        return rightJustify(s, maxLength, ' ');
    }

    /**
     * <P>Right justify a string using the specified padding character to pad
     * the left part of the string until it reaches <code>maxLength</code>.
     * If the length of the string is greater than <code>maxLength</code> characters,
     * return only the first, left <code>maxLength</code> characters.</P>
     *
     * @return   The right-justified string.
     */
    public static String rightJustify(String s, int maxLength, char fill) {

        if (s == null || maxLength == 0) {
            return s;
        }
        // If the string has more than "maxLength" characters, 
        // return only the first "maxLength" characters of the string. 
        if (s.trim().length() > maxLength) {
            return s.substring(0, maxLength).trim();
        }

        StringBuffer sb = new StringBuffer(s.trim());

        // Insert as many padding characters as needed to reach "maxLength".
        while (sb.length() < maxLength) {
            sb.insert(0, fill);
        }

        return sb.toString();
    }

    /** 
     * Left justify a string, and fill to a given length with the blanks.
     * If the length of the string is greater than "maxLength" characters, return only 
     * the first, left "maxLength" characters. 
     */
    public static String leftJustify(String s, int maxLength) {
        return leftJustify(s, maxLength, ' ');
    }

    /** 
     * Left justify a string, and fill to a given length with the character <code>fill</code>.
     * If the length of the string is greater than "maxLength" characters, return only 
     * the first, left "maxLength" characters. 
     */
    public static String leftJustify(String s, int maxLength, char fill) {
        if (s == null || maxLength == 0) {
            return s;
        }
        // If the string has more than "maxLength" characters, 
        // return only the first "maxLength" characters of the string. 
        if (s.trim().length() > maxLength) {
            return s.substring(0, maxLength).trim();
        }

        StringBuffer sb = new StringBuffer(s.trim());

        // Append as many blanks as needed to reach "maxLength". 
        while (sb.length() < maxLength) {
            sb.append(fill);
        }

        return sb.toString();

    }

    /**
     * Replace the first occurrence of <code>oldSubstring</code> in
     * <code>text</code>, if there is one, with <code>newSubstring</code>.
     *
     * @param text - Replace a substring of this String
     * @param oldSubstring - The substring of <code>text</code> to be replaced
     * @param newSubstring - The string to put into <code>text</code>
     * @return A new String which is a copy of <code>text</code> with the first
     *         occurrence of <code>oldSubstring</code> in <code>text</code>, if
     *         there is one, with <code>newSubstring</code>.
     *         Returns null if <code>text</code> is null.
     *         Returns <code>text</code> if either substring is null or
     *         <code>oldSubstring</code> is empty
     */
    public static String replace(String text, String oldSubstring, String newSubstring) {
        String result = text;

        if ((text != null) && (text.length() > 0) && (oldSubstring != null) && (oldSubstring.length() > 0)
                && (newSubstring != null)) {
            int pos = text.indexOf(oldSubstring);
            if (pos > -1) {
                result = text.substring(0, pos) + newSubstring + text.substring(pos + oldSubstring.length());
            }
        }

        return result;
    }

    /**
     * Replaces all occurrences of <code>oldSubstring</code> in
     * <code>text</code>, if there are any, with <code>newSubstring</code>.
     *
     * @param text - Replace substrings of this String
     * @param oldSubstring - The substring of <code>text</code> to be replaced
     * @param newSubstring - The string to put into <code>text</code>
     * @return A new String which is a copy of <code>text</code> with all
     *         occurrences of <code>oldSubstring</code> in <code>string</code>,
     *         if there are any, with <code>newSubstring</code>.
     *         Returns null if <code>text</code> is null.
     *         Returns <code>text</code> if either substring is null or
     *         <code>oldSubstring</code> is empty
     */
    public static String replaceAll(String text, String oldSubstring, String newSubstring) {
        String result = text;

        if ((result != null) && (result.length() > 0) && (result.indexOf(oldSubstring) > -1)
                && (oldSubstring.length() > 0) && (!oldSubstring.equals(newSubstring))) {

            while (result.indexOf(oldSubstring) > -1) {
                result = replace(result, oldSubstring, newSubstring);
            }
        }

        return result;
    }

    /**
     * Convert the String s to proper case.
     * 
     * @param s String
     * @return String
     */
    public static String toProperCase(String s) {
        StringBuilder sb = new StringBuilder();

        for (String f : s.split(" ")) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(f.substring(0, 1).toUpperCase()).append(f.substring(1, f.length()).toLowerCase());
        }

        return sb.toString();
    }
}