Java Integer Create toInt(String str)

Here you can find the source of toInt(String str)

Description

to Int

License

Apache License

Parameter

Parameter Description

Declaration

public static int toInt(String str) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w.j a va2  s .com*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
    * 
    * @use string convert int    defaultValue is -1
    * @param
    * @return
    */
    public static int toInt(String str) {
        return toInt(str, -1);
    }

    /**
    * 
    * @author liyixiang
    * @use  string convert int  if convert throws exception,use defaultValue
    * @param
    * @return
    */
    public static int toInt(String str, int defaultValue) {
        if (isBlank(str)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (Exception ex) {
            // ignore this exception
        }
        return defaultValue;
    }

    /**
    * <p>Checks if a String is whitespace, empty ("") or null.</p>
    *
    * <pre>
    * StringUtils.isBlank(null)      = true
    * StringUtils.isBlank("")        = true
    * StringUtils.isBlank(" ")       = true
    * StringUtils.isBlank("bob")     = false
    * StringUtils.isBlank("  bob  ") = false
    * </pre>
    *
    * @param str  the String to check, may be null
    * @return <code>true</code> if the String is null, empty or whitespace
    * @since 2.0
    */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
    * 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();
    }

    /**
    * <p>Checks if the String contains only whitespace.</p>
    *
    * <p><code>null</code> will return <code>false</code>.
    * An empty String (length()=0) will return <code>true</code>.</p>
    *
    * <pre>
    * StringUtils.isWhitespace(null)   = false
    * StringUtils.isWhitespace("")     = true
    * StringUtils.isWhitespace("  ")   = true
    * StringUtils.isWhitespace("abc")  = false
    * StringUtils.isWhitespace("ab2c") = false
    * StringUtils.isWhitespace("ab-c") = false
    * </pre>
    *
    * @param str  the String to check, may be null
    * @return <code>true</code> if only contains whitespace, and is non-null
    * @since 2.0
    */
    public static boolean isWhitespace(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. toInt(String s)
  2. toInt(String s)
  3. toInt(String s, int defaultInt)
  4. toInt(String s, int exception)
  5. toInt(String sInt)
  6. toInt(String str)
  7. toInt(String str)
  8. toInt(String str)
  9. toInt(String str)