版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、1 字符串轉(zhuǎn)換成數(shù)據(jù)字符串轉(zhuǎn)換成整數(shù):String MyNumber ="1234"int MyInt = Integer.parseInt(MyNumber;字符串轉(zhuǎn)換成byte, short, int, float, double, long等數(shù)據(jù)類型,可以分別參考Byte, Short, Integer, Float, Double, Long類的parseXXX方法。A. 有兩個方法:1. int i = Integer.parseInt(String; 或i = Integer.parseInt(String,int radix;2. int i = Intege
2、r.valueOf(my_Value(;注: 字串轉(zhuǎn)成Double, Float, Long 的方法大同小異.2 數(shù)據(jù)轉(zhuǎn)換成字符串整數(shù)轉(zhuǎn)換成字符串:int MyInt = 1234;String MyString = "" + MyInt;其它數(shù)據(jù)類型可以利用同樣的方法轉(zhuǎn)換成字符串。1. String s = String.valueOf(i;2. String s = Integer.toString(i;3. String s = "" + i;注: Double, Float, Long 轉(zhuǎn)成字串的方法大同小異.3 十進(jìn)制到其他進(jìn)制的
3、轉(zhuǎn)換十進(jìn)制整數(shù)轉(zhuǎn)換成二進(jìn)制整數(shù),返回結(jié)果是一個字符串:Integer.toBinaryString(int i;Integer和Long提供了toBinaryString, toHexString和toOctalString方法,可以方便的將數(shù)據(jù)轉(zhuǎn)換成二進(jìn)制、十六進(jìn)制和八進(jìn)制字符串。功能更加強(qiáng)大的是其toString(int/long i, int radix方法,可以將一個十進(jìn)制數(shù)轉(zhuǎn)換成任意進(jìn)制的字符串形式。byte, short, float和double等數(shù)據(jù)類型,可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString
4、和toString方法轉(zhuǎn)換成其他進(jìn)制的字符串形式。4 其它進(jìn)制到十進(jìn)制的轉(zhuǎn)換五進(jìn)制字符串14414轉(zhuǎn)換成十進(jìn)制整數(shù),結(jié)果是1234:System.out.println(Integer.valueOf("14414", 5;Integer和Long提供的valueOf(String source, int radix方法,可以將任意進(jìn)制的字符串轉(zhuǎn)換成十進(jìn)制數(shù)據(jù)。5 整數(shù)到字節(jié)數(shù)組的轉(zhuǎn)換public static byte toByteArray(int numberint temp = number;byte b=new byte4;for (int i = b.lengt
5、h - 1; i > -1; i-bi = new Integer(temp & 0xff.byteValue(;temp = temp >> 8;return b;6 字節(jié)數(shù)組到整數(shù)的轉(zhuǎn)換public static int toInteger(byte bint s = 0;for (int i = 0; i < 3; i+if (bi > 0s = s + bi;elses = s + 256 + bi;s = s * 256;if (b3 > 0s = s + b3;elses = s + 256 + b3;return s;7 短整數(shù)與字節(jié)數(shù)
6、組之間的相互轉(zhuǎn)換short與int之間的區(qū)別在于short是兩個字節(jié)的,而int是四個字節(jié)的。因此,只需要將5 與6 中的范例程序小做改動,即可實(shí)現(xiàn)短整數(shù)與字節(jié)數(shù)組之間的相互轉(zhuǎn)換。8 字節(jié)數(shù)組轉(zhuǎn)換成雙精度浮點(diǎn)數(shù)public double toDouble(byte blong l = 0;Double D = new Double(0.0;l = b0;l |= (longb1<<8;l |= (longb2<<16;l |= (longb3<<24;l |= (longb4<<32;l |= (longb5<<40;l |= (lo
7、ngb6<<48;l |= (longb7<<56;return D.longBitsToDouble(l;9 布爾類型轉(zhuǎn)換成字符串第一種方法是:boolean bool = true;String s = new Boolean(bool.toString(;/將bool利用對象封裝器轉(zhuǎn)化為對象s.equals("true"/* 其中,toString方法是一個繼承方法。java中所有的類都是object的繼承,object的一個重要方法就是toString,用于將對象轉(zhuǎn)化為字符串。*/第二種方法是:boolean bool = true;Stri
8、ng s = String.valueOf( bool ;首先,從代碼長度上講第二種方法明顯要比第一種方法簡潔;其次,第一種方法在轉(zhuǎn)化過程中多引入了一個完全沒有必要的對象,因此,相對第二種方法來說這就造成了內(nèi)存空間的浪費(fèi),大大減慢了運(yùn)行速度。所以,推薦使用第二種方法。10 數(shù)字類型與數(shù)字類對象之間的轉(zhuǎn)換byte b = 169;Byte bo = new Byte( b ;b = bo.byteValue(;short t = 169;Short to = new Short( t ;t = to.shortValue(;int i = 169;Integer io = new Integer
9、( i ;i = Value(;long l = 169;Long lo = new Long( l ;l = lo.longValue(;float f = 169f;Float fo = new Float( f ;f = fo.floatValue(;double d = 169f;Double dObj = new Double( d ;d = dObj.doubleValue(;* *package .lwkj.erts.register;import java.sql.Date;public class TypeChange public TypeChange( /ch
10、ange the string type to the int typepublic static int stringToInt(String intstrInteger integer;integer = Integer.valueOf(intstr;return Value(;/change int type to the string typepublic static String intToString(int valueInteger integer = new Integer(value;return integer.toString(;/change t
11、he string type to the float typepublic static float stringToFloat(String floatstrFloat floatee;floatee = Float.valueOf(floatstr;return floatee.floatV alue(;/change the float type to the string typepublic static String floatToString(float valueFloat floatee = new Float(value;return floatee.toString(;
12、/change the string type to the sqlDate typepublic static java.sql.Date stringToDate(String dateStr return java.sql.Date.valueOf(dateStr;/change the sqlDate type to the string typepublic static String dateToString(java.sql.Date datee return datee.toString(;public static void main(String argsjava.sql.
13、Date day ;day = TypeChange.stringToDate("2003-11-3"String strday = TypeChange.dateToString(day;System.out.println(strday;JA V A中常用數(shù)據(jù)類型轉(zhuǎn)換函數(shù)雖然都能在JA VA API中找到,整理一下做個備份。string->byteByte static byte parseByte(String sbyte->stringByte static String toString(byte bchar->stringCharacter s
14、tatic String to String (char cstring->ShortShort static Short parseShort(String sShort->StringShort static String toString(Short sString->Integer Integer static int parseInt(String s Integer->String Integer static String tostring(int i String->Long Long static long parseLong(String s
15、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 * * string->byte Byte
16、 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 Lo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 合同大寫金額標(biāo)準(zhǔn)寫法
- DB6103T 83-2025巨菌草栽培技術(shù)規(guī)范
- 產(chǎn)業(yè)園區(qū)綜合服務(wù)與物業(yè)管理合同
- 2025年環(huán)保技術(shù)項(xiàng)目研發(fā)合同
- 上海居民住房租賃合同細(xì)則
- 專利許可使用權(quán)轉(zhuǎn)讓合同
- 2025年借款合同范本:生活資金周轉(zhuǎn)專用
- 產(chǎn)權(quán)清楚的商業(yè)車位買賣合同
- 三方人才派遣合同模板
- 互聯(lián)網(wǎng)眾籌合作合同范本(修訂)
- 如愿三聲部合唱簡譜
- 高三數(shù)學(xué)開學(xué)第一課
- 水生野生動物保護(hù)與管理
- 115個低風(fēng)險(xiǎn)組病種目錄
- 系統(tǒng)解剖學(xué)考試重點(diǎn)筆記
- 暖通空調(diào)基礎(chǔ)知識及識圖課件
- 防滲墻工程施工用表及填寫要求講義
- 交通信號控制系統(tǒng)檢驗(yàn)批質(zhì)量驗(yàn)收記錄表
- 校園信息化設(shè)備管理檢查表
- 新版抗拔樁裂縫及強(qiáng)度驗(yàn)算計(jì)算表格(自動版)
- API SPEC 5DP-2020鉆桿規(guī)范
評論
0/150
提交評論