Java String to convertString(String s, Class cls)

Here you can find the source of convertString(String s, Class cls)

Description

Convert a string into an object of given type.

License

Open Source License

Parameter

Parameter Description
s a string
cls the type to convert the string to

Return

object of given type

Declaration

public static Object convertString(String s, Class<?> cls) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w  ww  .j  av a2s  . c  o  m
     * Convert a string into an object of given type.
     * 
     * @param s    a string
     * @param cls  the type to convert the string to
     * @return object of given type
     */
    public static Object convertString(String s, Class<?> cls) {
        if (cls == Integer.class) {
            return Integer.valueOf(s);
        } else if (cls == String.class) {
            return s;
        } else {
            throw new IllegalArgumentException("Don't know how to convert string to " + cls.getName());
        }
    }
}

Related

  1. convertString(char firstCharacter, String value)
  2. convertString(Object obj, String nullTo)
  3. convertString(String label)
  4. convertString(String s)
  5. convertString(String strValue)
  6. convertString(String value, Class type)
  7. convertString2Bytes(String str)
  8. convertString2GSonString(String str)