Java String to convertStringToPrimitive(Object object, Class toType)

Here you can find the source of convertStringToPrimitive(Object object, Class toType)

Description

convert String To Primitive

License

Open Source License

Declaration

public static Object convertStringToPrimitive(Object object, Class toType) 

Method Source Code

//package com.java2s;
/**//from  w  ww  . j a  v a 2s. c  o m
 *
 * Licensed under the GNU General Public License (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.gnu.org/licenses/gpl.txt
 *
 * 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.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

public class Main {
    public static Object convertStringToPrimitive(Object object, Class toType) {
        if (toType == boolean.class || toType == Boolean.class) {
            return Boolean.valueOf((String) object);
        }
        if (toType == char.class || toType == Character.class) {
            return new Character(((String) object).charAt(0));
        }
        if (toType == byte.class || toType == Byte.class) {
            return new Byte((String) object);
        }
        if (toType == short.class || toType == Short.class) {
            return new Short((String) object);
        }
        if (toType == int.class || toType == Integer.class) {
            return new Integer((String) object);
        }
        if (toType == long.class || toType == Long.class) {
            return new Long((String) object);
        }
        if (toType == float.class || toType == Float.class) {
            return new Float((String) object);
        }
        if (toType == double.class || toType == Double.class) {
            return new Double((String) object);
        }
        return null;
    }
}

Related

  1. convertStringToId(String s)
  2. convertStringToIntDef(String string, int defaultValue)
  3. convertStringToLiteral(String className, String value)
  4. convertStringToObject(String value, Class type)
  5. convertStringToPrimaryKey(String keyString)
  6. convertStringToPrimitive(String value, Class destinationClass)
  7. convertStringToSafeFilename(String s)
  8. convertStringToValue(String txt, Class klass)
  9. convertStringToVarName(final String str)