Conversion of String
Let's first introduce the conversion of string type, generally encountered in the following situations may be: strng int,string to Long,string to the byte array, string to float, the following main description of these four cases. string int
To convert a string type to an int type, the following three methods are commonly used:
1 2 3 4 5 6 7 8 |
public class Stringtoint {public static void main (string[] args) {String number = ' 123456 '; int num1 = Integer.parseint ( number); Use integer parseint method int num2 = new Integer (number); cast int num3 = integer.valueof (number). Intvalue (); First turn to the integer type, then call Intvalue () to int}} |
string to Long
The method of converting a string type to a long type is similar to the above method.
1 2 3 4 5 6 7 8 |
public class Stringtolong {public static void main (string[] args) {String number = ' 1234567890 '; long num1 = Long.parsel Ong (number); Call the long Parselong method Long num2 = new Long (number); Cast Long num3 = long.valueof (number). Longvalue (); Converts the long type first, then using the Longvalue method to the long} |
String Float
The method of converting a string type to a float type is similar to the above.
1 2 3 4 5 6 7 8 |
public class Stringtofloat {public static void main (string[] args) {String number = ' 1234.202 '; float num1 = Float.parse Float (number); Invoke float parsefloat method float num2 = new float (number); Cast float num3 = float.valueof (number). Floatvalue (); Convert to float type and then use Floatvalue to float}} |
string Turn byte[]
A string-type byte array method generally uses the GetBytes () method that is taken by the string class.
1 2 3 4 5 6 7 |
public class Stringtobyte {public static void main (string[] args) {byte[] num = new byte[200]; String number = "1234567890"; num = Number.getbytes (); } } |
This adds a way to convert a path type to a string type:
1 |
String filename=path.getfilename (). toString (); |
Long type conversion
Long type conversions, which are also used in a number of situations, are described in a few common cases below. Long Turn string
The long type is a string type, here are three methods:
1 2 3 4 5 6 7 8 |
public class Longtostring {public static void main (string[] args) {long number = 1234567890l; String NUM1 = long.tostring (number); The ToString method of a long String num2 = string.valueof (number); Use string valueof method string num3 = "" + number; This should belong to the cast bar} |
Long Round int
The long type is converted to type int, and here are three methods:
1 2 3 4 5 6 7 8 |
public class Longtoint {public static void main (string[] args) {long number = 121121121l; int num1 = (int) number;//strong type conversion int num2 = new Long (number). Intvalue (); Call Intvalue method int num3 = Integer.parseint (string.valueof (number)); Converts a long to a string string and then to an integer} |
conversion between long and byte arrays
I've always felt that byte array conversion is cumbersome, and this is no longer described here, I gave a blog for others to let you as a reference to it, where the byte array and a variety of data types of conversion--java byte and each data type (short,int,long,float,double) Conversion of int type between
The conversion of type int is also the case that we often use, and the following are mainly about several common situations. int Turn string
The int type is converted to a string similar to a long string, and generally has the following three methods.