| Start |
|---|
| Converting String to other datatypes. You can convert a numeric string to various Java numeric types as follows: String to int: String to float: String to double: String to boolean: String to long: String to BigInteger: String to BigDecimal: Conversion Exceptions: The numeric conversions above will all throw an (unchecked) If you wanted to test that you can parse a string, you could implement a However, calling this |
| Conversion to / from bytes To encode a string into a byte array, you can simply use the and to decode: you can further simplify the call by using a static import: For less common character sets you can indicate the character set with a string: and the reverse: this does however mean that you have to handle the checked 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. and the reverse: 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 |
| Base64 Encoding / Decoding Occasionally you will find the need to encode binary data as a base64 (opens new window)-encoded string. For this we can use the Apache commons-codec Alternatively, we can use If you inspect this program wile running, you will see that Details for the same can be found at Base64 (opens new window) |
Converting other datatypes to String This method is also overloaded for other datatypes, such as .toString on it. For this to give useful output, the class must override toString(). Most of the standard Java library classes do, such as Date and others.
For example: Here You can also convert any number type to String with short notation like below. Or just simple way is |
Getting a String from an InputStream A This uses the system default charset, although an alternate charset may be specified: |
| Parsing Strings to a Numerical Value String to a primitive numeric type or a numeric wrapper type: Each numeric wrapper class provides a |
Read Article
https://develop.horje.com/learn/1434/reference