java中string和int互相轉(zhuǎn)化
1 如何將字串 String 轉(zhuǎn)換成整數(shù) int?
A. 有兩個(gè)方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串轉(zhuǎn)成 Double, Float, Long 的方法大同小異.
2 如何將整數(shù) int 轉(zhuǎn)換成字串 String ?
A. 有叁種方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 轉(zhuǎn)成字串的方法大同小異.
int -> String
int i=12345;
String s="";
第一種方法:s=i+"";
第二種方法:s=String.valueOf(i);
這兩種方法有什么區(qū)別呢?作用是不是一樣的呢?是不是在任何下都能互換呢?
String -> int
s="12345";
int i;
第一種方法:i=Integer.parseInt(s);
第二種方法:i=Integer.valueOf(s).intValue();
這兩種方法有什么區(qū)別呢?作用是不是一樣的呢?是不是在任何下都能互換呢?
以下是答案:
第一種方法:s=i+""; //會(huì)產(chǎn)生兩個(gè)String對(duì)象
第二種方法:s=String.valueOf(i); //直接使用String類(lèi)的靜態(tài)方法,只產(chǎn)生一個(gè)對(duì)象
第一種方法:i=Integer.parseInt(s);//直接使用靜態(tài)方法,不會(huì)產(chǎn)生多余的對(duì)象,但會(huì)拋出異常
第二種方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相當(dāng)于 new Integer(Integer.parseInt(s)),也會(huì)拋
異常,但會(huì)多產(chǎn)生一個(gè)對(duì)象
--------------------------------------------------------------------
1如何將字串 String 轉(zhuǎn)換成整數(shù) int?
A. 有兩個(gè)方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串轉(zhuǎn)成 Double, Float, Long 的方法大同小異.
2 如何將整數(shù) int 轉(zhuǎn)換成字串 String ?
A. 有叁種方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 轉(zhuǎn)成字串的方法大同小異.
JAVA數(shù)據(jù)類(lèi)型轉(zhuǎn)換
關(guān)鍵字 類(lèi)型轉(zhuǎn)換
這是一個(gè)例子,說(shuō)的是JAVA中數(shù)據(jù)數(shù)型的轉(zhuǎn)換.供大家學(xué)習(xí)引
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);
}
}
JAVA中常用數(shù)據(jù)類(lèi)型轉(zhuǎn)換函數(shù)
雖然都能在JAVA API中找到,整理一下做個(gè)備份。
C++ int與string的轉(zhuǎn)化
int本身也要用一串字符表示,前后沒(méi)有雙引號(hào),告訴編譯器把它當(dāng)作一個(gè)數(shù)解釋。缺省情況下,是當(dāng)成10進(jìn)制(dec)來(lái)解釋?zhuān)绻胗?進(jìn)制,16進(jìn)制,怎么辦?加上前綴,告訴編譯器按照不同進(jìn)制去解釋。8進(jìn)制(oct)---前綴加0,16進(jìn)制(hex)---前綴加0x或者0X。
string前后加上雙引號(hào),告訴編譯器把它當(dāng)成一串字符來(lái)解釋。
注意:對(duì)于字符,需要區(qū)分字符和字符表示的數(shù)值。比如:char a = 8;char b = '8',a表示第8個(gè)字符,b表示字符8,是第56個(gè)字符。
int轉(zhuǎn)化為string
1、使用itoa(int to string)
//char *itoa( int value, char *string,int radix);
// 原型說(shuō)明:
// value:欲轉(zhuǎn)換的數(shù)據(jù)。
// string:目標(biāo)字符串的地址。
// radix:轉(zhuǎn)換后的進(jìn)制數(shù),可以是10進(jìn)制、16進(jìn)制等。
// 返回指向string這個(gè)字符串的指針
int aa = 30;
char c[8];
itoa(aa,c,16);
cout<<c<<endl; // 1e
注意:itoa并不是一個(gè)標(biāo)準(zhǔn)的C函數(shù),它是Windows特有的,如果要寫(xiě)跨平臺(tái)的程序,請(qǐng)用sprintf。
2、使用sprintf
// int sprintf( char *buffer, const char *format, [ argument] … );
//參數(shù)列表
// buffer:char型指針,指向?qū)⒁獙?xiě)入的字符串的緩沖區(qū)。
// format:格式化字符串。
// [argument]...:可選參數(shù),可以是任何類(lèi)型的數(shù)據(jù)。
// 返回值:字符串長(zhǎng)度(strlen)
int aa = 30;
char c[8];
int length = sprintf(c, "%05X", aa);
cout<<c<<endl; // 0001E
3、使用stringstream
int aa = 30;
stringstream ss;
ss<<aa;
string s1 = ss.str();
cout<<s1<<endl; // 30
string s2;
ss>>s2;
cout<<s2<<endl; // 30
可以這樣理解,stringstream可以吞下不同的類(lèi)型,根據(jù)s2的類(lèi)型,然后吐出不同的類(lèi)型。
4、使用boost庫(kù)中的lexical_cast
int aa = 30;
string s = boost::lexical_cast<string>(aa);
cout<<s<<endl; // 30
3和4只能轉(zhuǎn)化為10進(jìn)制的字符串,不能轉(zhuǎn)化為其它進(jìn)制的字符串。
string轉(zhuǎn)化為int
1、使用strtol(string to long)
string s = "17";
char* end;
int i = static_cast<int>(strtol(s.c_str(),&end,16));
cout<<i<<endl; // 23
i = static_cast<int>(strtol(s.c_str(),&end,10));
cout<<i<<endl; // 17
2、使用sscanf
int i;
sscanf("17","%D",&i);
cout<<i<<endl; // 17
sscanf("17","%X",&i);
cout<<i<<endl; // 23
sscanf("0X17","%X",&i);
cout<<i<<endl; // 23
3、使用stringstream
string s = "17";
stringstream ss;
ss<<s;
int i;
ss>>i;
cout<<i<<endl; // 17
注:stringstream可以吞下任何類(lèi)型,根據(jù)實(shí)際需要吐出不同的類(lèi)型。
4、使用boost庫(kù)中的lexical_cast
string s = "17";
int i = boost::lexical_cast<int>(s);
cout<<i<<endl; // 17