Java String Empty to Null emptyToNull(String s)

Here you can find the source of emptyToNull(String s)

Description

empty To Null

License

Open Source License

Declaration

public static String emptyToNull(String s) 

Method Source Code

//package com.java2s;
/*//from w  ww . j a  v  a2  s . c o m
 * (c) Copyright 2007-2012 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

public class Main {
    public static String emptyToNull(String s) {
        if (s == null || s.length() == 0)
            return null;
        String trimmed = trim(s);
        return (trimmed.length() != 0 ? s : null);
    }

    /** Trims a String by removing white spaces (including nbsp) from left and right. */
    public static String trim(String s) {
        if (s == null || s.length() == 0)
            return s;
        int beginIndex;
        for (beginIndex = 0; beginIndex < s.length() && isWhitespace(s.charAt(beginIndex)); beginIndex++) {
        }
        int endIndex;
        for (endIndex = s.length() - 1; endIndex > 0 && isWhitespace(s.charAt(endIndex)); endIndex--) {
        }
        if (beginIndex > endIndex)
            return "";
        return s.substring(beginIndex, endIndex + 1);
    }

    public static String trim(String source, char padChar) {
        if (source == null)
            return null;
        int i0 = 0;
        while (i0 < source.length() && source.charAt(i0) == padChar)
            i0++;
        if (i0 == source.length())
            return "";
        int i1 = source.length() - 1;
        while (i1 > i0 && source.charAt(i1) == padChar)
            i1--;
        return source.substring(i0, i1 + 1);
    }

    /** interprets an nbsp as space character */
    public static boolean isWhitespace(char c) {
        return Character.isWhitespace(c) || c == 160;
    }
}

Related

  1. emptyStringToNull(String string)
  2. emptyToDefault(String value, String def)
  3. emptyToDefault(T[] values, T[] def)
  4. emptyToNull(final String text, final boolean doTrim)
  5. emptyToNull(String field)
  6. emptyToNull(String s)
  7. emptyToNull(String str)
  8. emptyToNull(String str)
  9. emptyToNull(String str)