Java integer and String Conversion

Source: Internet
Author: User

1 how to convert a string to an integer Int?

A. There are two methods:

1). Int I = integer. parseint ([String]); or

I = integer. parseint ([String], [int Radix]);

2). Int I = integer. valueof (my_str). intvalue ();

Note: The methods for converting strings to double, float, and long are similar.

2. How do I convert an integer int to a string?

A. There are three methods:

1.) string S = string. valueof (I );

2.) string S = integer. tostring (I );

3.) string S = "" + I;

Note: the conversion of double, float, and long into strings is similar.

Conversion of Java data types to ynniebo [favorites]

Keyword type conversion

Source

This is an example of data type conversion in Java.

Package cn.com. lwkj. ERTs. Register;

Import java. SQL. date;

Public class typechange {

Public typechange (){

}

// Change the string type to the int type

Public static int stringtoint (string intstr)

{

Integer integer;

Integer = integer. valueof (intstr );

Return integer. intvalue ();

}

// Change int type to the string type

Public static string inttostring (INT value)

{

Integer integer = new INTEGER (value );

Return integer. tostring ();

}

// Change the string type to the Float Type

Public static float stringtofloat (string floatstr)

{

Float floatee;

Floatee = float. valueof (floatstr );

Return floatee. floatvalue ();

}

// Change the float type to the string type

Public static string floattostring (float value)

{

Float floatee = new float (value );

Return floatee. tostring ();

}

// Change the string type to the sqldate type

Public static java. SQL. Date stringtodate (string datestr)

{

Return java. SQL. Date. valueof (datestr );

}

// Change the sqldate type to the string type

Public static string datetostring (Java. SQL. Date datee)

{

Return datee. tostring ();

}

Public static void main (string [] ARGs)

{

Java. SQL. Date Day;

Day = typechange. stringtodate ("2003-11-3 ");

String strday = typechange. datetostring (day );

System. Out. println (strday );

}

}

Common data type conversion functions in Java

Although they can all be found in Java API, sort out and make a backup.

String-> byte

Byte static byte parsebyte (string S)

Byte-> string

Byte static string tostring (byte B)

Char-> string

Character static string to string (char C)

String-> short

Short static short parseshort (string S)

Short-> string

Short static string tostring (short S)

String-> integer

Integer static int parseint (string S)

Integer-> string

Integer static string tostring (int I)

String-> long

Long static long parselong (string S)

Long-> string

Long static string tostring (long I)

String-> float

Float static float parsefloat (string S)

Float-> string

Float static string tostring (float F)

String-> double

Double static double parsedouble (string S)

Double-> string

Double static string tostring (double D)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.