Gets a String's length or 0 if the String is null. - Java java.lang

Java examples for java.lang:String Length

Description

Gets a String's length or 0 if the String is null.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(length(str));
    }//from ww w.ja  va  2  s  .  c  om

    /**
     * Gets a String's length or <code>0</code> if the String is <code>null</code>.
     *
     * @param str
     *            a String or <code>null</code>
     * @return String length or <code>0</code> if the String is <code>null</code>.
     * @since 2.4
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }
}

Related Tutorials