Convert string to any type in java

Parse strings and convert the value to .java types

I should also give some usage examples. Use the below static method for simple usage (only supports the default java types):

Integer i1 = StringToTypeParser.parse(" 1", Integer.class); int i2 = StringToTypeParser.parse("2", int.class); float f = StringToTypeParser.parse("2.2", float.class); Number n = StringToTypeParser.parse("2.2", float.class); MyEnum a = StringToTypeParser.parse("BBB", MyEnum.class); File file = StringToTypeParser.parse("/path/to", File.class); Boolean b1 = StringToTypeParser.parse("true", Boolean.class); boolean b2 = StringToTypeParser.parse("0", Boolean.class); 

Or if you want to register your own TypeParsers, build your own StringToTypeParser instance (which by default supports the java types) as follow:

StringToTypeParser parser = StringToTypeParser.newBuilder() .registerTypeParser(new CarTypeParser()) .registerTypeParser(int.class, new MySpecialIntTypeParser()) .build(); Car volvo = parser.parseType("volvo", Car.class); 

Edit: after comments from review:

Thanks for the great comments in the answer! Below are the final outcome of the changes made. The Util.java class is now removed, so is also all the reflection code, which makes a much nicer/cleaner code.

The TypeParser interface remains the same

package com.github.drapostolos.typeparser; public interface TypeParser

Replaced the static inner classes with anonymous classes instead.

package com.github.drapostolos.typeparser; import java.io.File; import java.util.HashMap; import java.util.Map; class DefaultTypeParsers < private DefaultTypeParsers() < new AssertionError("Not meant for instantiation"); >private static final String BOOLEAN_ERROR_MESSAGE = "\"%s\" is not parsable to a Boolean."; private static final String CHARACTER_ERROR_MESSAGE = "\"%s\" must only contain a single character."; private static final Map, TypeParser> DEFAULT_TYPE_PARSERS = new HashMap, TypeParser>(); private static final Map, Class> WRAPPER_TO_PRIMITIVE = new HashMap, Class>(); static Map, TypeParser> copy() < return new HashMap, TypeParser>(DEFAULT_TYPE_PARSERS); > static < WRAPPER_TO_PRIMITIVE.put(Boolean.class, boolean.class); WRAPPER_TO_PRIMITIVE.put(Byte.class, byte.class); WRAPPER_TO_PRIMITIVE.put(Short.class, short.class); WRAPPER_TO_PRIMITIVE.put(Character.class, char.class); WRAPPER_TO_PRIMITIVE.put(Integer.class, int.class); WRAPPER_TO_PRIMITIVE.put(Long.class, long.class); WRAPPER_TO_PRIMITIVE.put(Float.class, float.class); WRAPPER_TO_PRIMITIVE.put(Double.class, double.class); >private static void put(Class type, TypeParser typeParser) < DEFAULT_TYPE_PARSERS.put(type, typeParser); if(WRAPPER_TO_PRIMITIVE.containsKey(type))< // add primitive targetType if existing, example int.class, boolean.class etc. ClassprimitiveType = WRAPPER_TO_PRIMITIVE.get(type); DEFAULT_TYPE_PARSERS.put(primitiveType, typeParser); > > static< put(Boolean.class, new TypeParser() < @Override public Boolean parse(final String value0) < String value = value0.trim().toLowerCase(); if(value.equals("true"))< return Boolean.TRUE; >else if(value.equals("false")) < return Boolean.FALSE; >throw new IllegalArgumentException(String.format(BOOLEAN_ERROR_MESSAGE, value0)); > >); put(Character.class, new TypeParser() < @Override public Character parse(String value) < if(value.length() == 1)< return Character.valueOf(value.charAt(0)); >throw new IllegalArgumentException(String.format(CHARACTER_ERROR_MESSAGE, value)); > >); put(Byte.class, new TypeParser() < @Override public Byte parse(String value) < return Byte.valueOf(value.trim()); >>); put(Integer.class, new TypeParser() < @Override public Integer parse(String value) < return Integer.valueOf(value.trim()); >>); put(Long.class, new TypeParser() < @Override public Long parse(String value) < return Long.valueOf(value.trim()); >>); put(Short.class, new TypeParser() < @Override public Short parse(String value) < return Short.valueOf(value.trim()); >>); put(Float.class, new TypeParser() < @Override public Float parse(String value) < return Float.valueOf(value); >>); put(Double.class, new TypeParser() < @Override public Double parse(String value) < return Double.valueOf(value); >>); put(File.class, new TypeParser() < @Override public File parse(String value) < return new File(value.trim()); >>); put(String.class, new TypeParser() < @Override public String parse(String value) < return value; >>); > > 

Removed the static StringToTypeParser.parse(. ) method for simplicity.

package com.github.drapostolos.typeparser; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; public final class StringToTypeParser < private static final Object STATIC_METHOD = null; private final Map, TypeParser> typeParsers; public static StringToTypeParserBuilder newBuilder() < return new StringToTypeParserBuilder(); >StringToTypeParser(Map, TypeParser> typeParsers) < this.typeParsers = Collections.unmodifiableMap(new HashMap, TypeParser>(typeParsers)); > public T parse(String value, Class type) < if (value == null) < throw new NullPointerException(nullArgumentErrorMsg("value")); >if (type == null) < throw new NullPointerException(nullArgumentErrorMsg("type")); >// convert "null" string to null type. if (value.trim().equalsIgnoreCase("null")) < if (type.isPrimitive()) < String message = "'%s' primitive can not be set to null."; throw new IllegalArgumentException(String.format(message, type.getName())); >return null; > Object result = null; if (typeParsers.containsKey(type)) < result = callTypeParser(value, type); >else if ((result = callFactoryMethodIfExisting("valueOf", value, type)) != null) < // >else if ((result = callFactoryMethodIfExisting("of", value, type)) != null) < // >else < String message = "There is no registered 'TypeParser' for that type, or that " + "type does not contain one of the following static factory methods: " + "'%s.valueOf(String)', or '%s.of(String)'."; message = String.format(message, type.getSimpleName(), type.getSimpleName()); message = canNotParseErrorMsg(value, type, message); throw new IllegalArgumentException(message); >/* * This cast is correct, since all above checks ensures we're casting to * the right type. */ @SuppressWarnings("unchecked") T temp = (T) result; return temp; > /** * This method is static because it is also called from . */ static String nullArgumentErrorMsg(String argName) < return String.format("Argument named '%s' is illegally set to null!", argName); >private Object callTypeParser(String value, Class type) < try < return typeParsers.get(type).parse(value); >catch (NumberFormatException e) < String message = canNotParseErrorMsg(value, type, numberFormatErrorMsg(e)); throw new IllegalArgumentException(message, e); >catch (RuntimeException e) < throw new IllegalArgumentException(canNotParseErrorMsg(value, type, e.getMessage()), e); >> private String numberFormatErrorMsg(NumberFormatException e) < return String.format("Number format exception %s.", e.getMessage()); >private String canNotParseErrorMsg(String value, Class type, String message) < return String.format("Can not parse \"%s\" to type '%s' due to: %s", value, type.getName(), message); >private Object callFactoryMethodIfExisting(String methodName, String value, Class type) < Method m; try < m = type.getDeclaredMethod(methodName, String.class); m.setAccessible(true); if (!Modifier.isStatic(m.getModifiers())) < // Static factory method does not exists, return null return null; >> catch (Exception e) < // Static factory method does not exists, return null return null; >try < if(type.isEnum())< value = value.trim(); >return m.invoke(STATIC_METHOD, value); > catch (InvocationTargetException e) < // filter out the InvocationTargetException stacktrace/message. throw new IllegalArgumentException(makeErrorMsg(methodName, value, type), e.getCause()); >catch (Throwable t) < throw new IllegalArgumentException(makeErrorMsg(methodName, value, type), t); >> private String makeErrorMsg(String methodName, String value, Class type) < String methodSignature = String.format("%s.%s('%s')", type.getName(), methodName, value); String message = " Exception thrown in static factory method '%s'. See underlying " + "exception for additional information."; return canNotParseErrorMsg(value, type, String.format(message, methodSignature)); >> 

Removed registerTypeParser(TypeParser) method, which allowed for removing reflection code.

package com.github.drapostolos.typeparser; import java.util.Map; public final class StringToTypeParserBuilder < private Map, TypeParser> typeParsers; StringToTypeParserBuilder() < // Initialize with the default typeParsers typeParsers = DefaultTypeParsers.copy(); >public StringToTypeParserBuilder unregisterTypeParser(Class type) < if(type == null) < throw new NullPointerException(StringToTypeParser.nullArgumentErrorMsg("type")); >typeParsers.remove(type); return this; > public StringToTypeParserBuilder registerTypeParser(Class type, TypeParser typeParser) < if(typeParser == null) < throw new NullPointerException(StringToTypeParser.nullArgumentErrorMsg("typeParser")); >if(type == null) < throw new NullPointerException(StringToTypeParser.nullArgumentErrorMsg("type")); >typeParsers.put(type, typeParser); return this; > public StringToTypeParser build() < return new StringToTypeParser(typeParsers); >> 

Источник

Читайте также:  Python лучший язык для машинного обучения

# Converting to and from Strings

You can convert a numeric string to various Java numeric types as follows:

String to int:

String number = "12"; int num = Integer.parseInt(number); 

String to float:

String number = "12.0"; float num = Float.parseFloat(number); 

String to double:

String double = "1.47"; double num = Double.parseDouble(double); 

String to boolean:

String falseString = "False"; boolean falseBool = Boolean.parseBoolean(falseString); // falseBool = false String trueString = "True"; boolean trueBool = Boolean.parseBoolean(trueString); // trueBool = true 

String to long:

String number = "47"; long num = Long.parseLong(number); 

String to BigInteger:

String bigNumber = "21"; BigInteger reallyBig = new BigInteger(bigNumber); 

String to BigDecimal:

String bigFraction = "17.21455"; BigDecimal reallyBig = new BigDecimal(bigFraction); 

Conversion Exceptions:

The numeric conversions above will all throw an (unchecked) NumberFormatException if you attempt to parse a string that is not a suitably formatted number, or is out of range for the target type. The Exceptions

(opens new window) topic discusses how to deal with such exceptions.

If you wanted to test that you can parse a string, you could implement a tryParse. method like this:

boolean tryParseInt (String value)  try  String somechar = Integer.parseInt(value); return true; > catch (NumberFormatException e)  return false; > > 

However, calling this tryParse. method immediately before parsing is (arguably) poor practice. It would be better to just call the parse. method and deal with the exception.

# Conversion to / from bytes

To encode a string into a byte array, you can simply use the String#getBytes() method, with one of the standard character sets available on any Java runtime:

byte[] bytes = "test".getBytes(StandardCharsets.UTF_8); 
String testString = new String(bytes, StandardCharsets.UTF_8); 

you can further simplify the call by using a static import:

import static java.nio.charset.StandardCharsets.UTF_8; ... byte[] bytes = "test".getBytes(UTF_8); 

For less common character sets you can indicate the character set with a string:

byte[] bytes = "test".getBytes("UTF-8"); 
String testString = new String (bytes, "UTF-8"); 

this does however mean that you have to handle the checked UnsupportedCharsetException .

The following call will use the default character set. The default character set is platform specific and generally differs between Windows, Mac and Linux platforms.

String testString = new String(bytes); 

Note that invalid characters and bytes may be replaced or skipped by these methods. For more control — for instance for validating input — you’re encouraged to use the CharsetEncoder and CharsetDecoder classes.

# Base64 Encoding / Decoding

Occasionally you will find the need to encode binary data as a base64

import javax.xml.bind.DatatypeConverter; import java.util.Arrays; // arbitrary binary data specified as a byte array byte[] binaryData = "some arbitrary data".getBytes("UTF-8"); // convert the binary data to the base64-encoded string String encodedData = DatatypeConverter.printBase64Binary(binaryData); // encodedData is now "c29tZSBhcmJpdHJhcnkgZGF0YQ==" // convert the base64-encoded string back to a byte array byte[] decodedData = DatatypeConverter.parseBase64Binary(encodedData); // assert that the original data and the decoded data are equal assert Arrays.equals(binaryData, decodedData); 

Apache commons-codec

Alternatively, we can use Base64 from Apache commons-codec

import org.apache.commons.codec.binary.Base64; // your blob of binary as a byte array byte[] blob = "someBinaryData".getBytes(); // use the Base64 class to encode String binaryAsAString = Base64.encodeBase64String(blob); // use the Base64 class to decode byte[] blob2 = Base64.decodeBase64(binaryAsAString); // assert that the two blobs are equal System.out.println("Equal : " + Boolean.toString(Arrays.equals(blob, blob2))); 

If you inspect this program wile running, you will see that someBinaryData encodes to c29tZUJpbmFyeURhdGE= , a very managable UTF-8 String object.

Details for the same can be found at Base64

// encode with padding String encoded = Base64.getEncoder().encodeToString(someByteArray); // encode without padding String encoded = Base64.getEncoder().withoutPadding().encodeToString(someByteArray); // decode a String byte [] barr = Base64.getDecoder().decode(encoded); 

# Converting other datatypes to String

int i = 42; String string = String.valueOf(i); //string now equals "42”. 
Foo foo = new Foo(); //Any class. String stringifiedFoo = foo.toString(). 

You can also convert any number type to String with short notation like below.

# Getting a String from an InputStream

A String can be read from an InputStream using the byte array constructor.

import java.io.*; public String readString(InputStream input) throws IOException  byte[] bytes = new byte[50]; // supply the length of the string in bytes here input.read(bytes); return new String(bytes); > 

This uses the system default charset, although an alternate charset may be specified:

return new String(bytes, Charset.forName("UTF-8")); 

# Parsing Strings to a Numerical Value

String to a primitive numeric type or a numeric wrapper type:

Each numeric wrapper class provides a parseXxx method that converts a String to the corresponding primitive type. The following code converts a String to an int using the Integer.parseInt method:

String string = "59"; int primitive = Integer.parseInteger(string); 

To convert to a String to an instance of a numeric wrapper class you can either use an overload of the wrapper classes valueOf method:

String string = "59"; Integer wrapper = Integer.valueOf(string); 

or rely on auto boxing (Java 5 and later):

String string = "59"; Integer wrapper = Integer.parseInteger(string); // 'int' result is autoboxed 

The above pattern works for byte , short , int , long , float and double and the corresponding wrapper classes ( Byte , Short , Integer , Long , Float and Double ).

String to Integer using radix:

String integerAsString = "0101"; // binary representation int parseInt = Integer.parseInt(integerAsString,2); Integer valueOfInteger = Integer.valueOf(integerAsString,2); System.out.println(valueOfInteger); // prints 5 System.out.println(parseInt); // prints 5 

(opens new window) exception will be thrown if a numeric valueOf(String) or parseXxx(. ) method is called for a string that is not an acceptable numeric representation, or that represents a value that is out of range.

Источник

Вопрос-ответ: как в Java правильно конвертировать String в int?

Java-университет

int в String — очень просто, и вообще практически любой примитивный тип приводится к String без проблем.

 int x = 5; String text = "X lang-java line-numbers">int i = Integer.parseInt (myString);

Если строка, обозначенная переменной myString , является допустимым целым числом, например «1», «200», Java спокойно преобразует её в примитивный тип данных int . Если по какой-либо причине это не удается, подобное действие может вызвать исключение NumberFormatException , поэтому чтобы программа работала корректно для любой строки, нам нужно немного больше кода. Программа, которая демонстрирует метод преобразования Java String в int , управление для возможного NumberFormatException :

 public class JavaStringToIntExample < public static void main (String[] args) < // String s = "fred"; // используйте это, если вам нужно протестировать //исключение ниже String s = "100"; try < // именно здесь String преобразуется в int int i = Integer.parseInt(s.trim()); // выведем на экран значение после конвертации System.out.println("int i = " + i); >catch (NumberFormatException nfe) < System.out.println("NumberFormatException: " + nfe.getMessage()); >> 

Обсуждение

Когда вы изучите пример выше, вы увидите, что метод Integer.parseInt (s.trim ()) используется для превращения строки s в целое число i , и происходит это в следующей строке кода:

int i = Integer.parseInt (s.trim ())
  • Integer.toString (int i) используется для преобразования int в строки Java.
  • Если вы хотите преобразовать объект String в объект Integer (а не примитивный класс int ), используйте метод valueOf () для класса Integer вместо метода parseInt () .
  • Если вам нужно преобразовать строки в дополнительные примитивные поля Java, используйте такие методы, как Long.parseLong () и ему подобные.

Источник

Оцените статью