浪潮優(yōu)派培訓(xùn)java筆記:第7章 String_第1頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、浪潮優(yōu)派培訓(xùn)java筆記:第7章 string第7章 sing 7.1 基本數(shù)據(jù)類型 string不行以被繼承,由于java.lang.string是一個(gè)final類。(面試??迹?public class teststring public ic vo main(stringargs) string s1= hello string s2= hello system.out.print(s1=s2); /true;s1、s2都指向常量區(qū)的字符串常量 hello string s3=new string( hello string s4=new string( hello system.out

2、.println(s3=s4); /fae;s3、s4指向不同的string對象 system.out.println(s1=s3); /false system.out.println(s3.equals(s4);/true;比較的是所指對象的 而非地址 system.out.println(s1.equals(s3);/false string是 傳遞,stringbuffer是引用傳遞 public class j_test_4_11 public static void mb_method(strings, stringbuffer t) s =s.replace('j'

3、;, 'i'); t = t.append( c public static void main(stringargs) string a = new string( java stringbufferb = new stringbuffer( java mb_method(a,b); system.out.println( a: a , b: b); 【運(yùn)行結(jié)果】a:java, b:javac public class j_test_4_13 public static voidmb_method(stringbuffer x, stringbuffer y) x.appen

4、d(y); y = x; /把x的地址賦給y public static void main(stringargs) stringbuffera = new stringbuffer( a stringbufferb = new stringbuffer( b mb_method(a,b); system.out.println(a , b); 【運(yùn)行結(jié)果】ab,b string類的常用辦法(細(xì)節(jié)參見協(xié)助文檔) int length();/獵取長度 char charat(int);/取得指定位置上的字符 char tochararray();/把指定字符串轉(zhuǎn)換為字符數(shù)組 byte getby

5、tes(); /轉(zhuǎn)換為字節(jié)數(shù)組 int compareto(string);/與另一個(gè)字符串按字典挨次比較大小,返回 為 0, =0, 0 三種狀況之一 boolean startswith(string);/推斷是否以指定的字符串打頭 boolean endswith(string);/推斷是否以指定的字符串結(jié)尾 string coat(string);/返回與另一個(gè)字符串銜接后的字符串 string bstring(intbegin);/可用于字符串截取 string substring(int begin,int end);/返回子串,注重不包括end位置上的字符 string trim

6、();/返回去除兩端空 后的字符串 string replace(char oldc,char ne); string replaceall(string olds,string news);/返回替換后的字符串 string tolowercase();/轉(zhuǎn)換為小寫 stringtouppercase(); /轉(zhuǎn)換為大寫 int indof(char/string); int indexof(char/string,int off);/返回字符/串首次浮現(xiàn)的位置,無時(shí)返回-1 int lastindexof(char/string); int lastindexof(char/string,

7、int offset);/返回字符/串末次浮現(xiàn)的位置,無時(shí)返回-1 static string valueof(xxx);/返回xxx的字符串表示,xxx普通取基本數(shù)據(jù)類型和字符數(shù)組 /用法它可以實(shí)現(xiàn)數(shù)字到字符串的轉(zhuǎn)換 【string類的常用辦法練習(xí)】 字符串t中的字符為( ) string s = hypertext string t = s.substring(2, 5); system.out.println(t); a. yper b. ype c. pert d. per 答案:d 解析:s.substring(2, 5);/從下標(biāo)為2的取到下標(biāo)為4的,注重取不到5. 輸入一個(gè)字符串

8、,顯示將該字符串中非字母字符刪除后的字符串。 public class deletestr public static void main(stringargs) string str = sj$% *asqekjf_sd/sdsd'fds3s1! string newstr= new string(); char a =str.tochararray(); for (int i = 0; i str.length(); i ) system.out.print(ai if (ai = 'a' ai = 'z' | ai = 'a' ai

9、 = 'z') newstr= newstr.con(string.valueof(ai); system.out.println(); system.out.println( 刪除后的字符串為: newstr); 【辦法提煉】 1. 將其它類型(如字符串)轉(zhuǎn)換成字符串形式:string.valueof(char c); 編寫一個(gè)程序,輸出一個(gè)字符串中的大寫英文字母數(shù),小寫英文字母數(shù)以及非英文字母數(shù)。 辦法一: public class countstring public static void main(stringargs) string s = i love china

10、!because i'm a chinese! int k = 0, m = 0, n =0; for (int i = 0; i s.length(); i ) char c = s.charat(i); /把字符串中的每一個(gè)字符賦給字符數(shù)組 if (character.isuppercase(c) /推斷是否是大寫字母(利用包裝類的辦法) k else if (character.islowercase(c) m else n system.out.println( 大寫字母個(gè)數(shù)有: k 個(gè) system.out.println( 小寫字母個(gè)數(shù)有: m 個(gè) system.out.p

11、rintln( 非字母個(gè)數(shù)有: n 個(gè) 辦法二: public class countstring public static void main(stringargs) string s = i love china!because i'm a chinese! int k = 0, m = 0, n =0; char c = s.tochararray(); for (int i = 0; i c.length; i ) if (ci = 65 ci = 90) k else if (ci = 97 ci = 122) m else n system.out.println( 大寫

12、字母個(gè)數(shù)有: k 個(gè) system.out.println( 小寫字母個(gè)數(shù)有: m 個(gè) system.out.println( 非字母個(gè)數(shù)有: n 個(gè) 編寫一個(gè)程序,輸出在一個(gè)字符串中,指定字符串浮現(xiàn)的次數(shù)。 public class findstrcount public static void main(string args) string str = i love china!because i'm a chinese! string substr = ch int count = 0; int index = -1; while (index = str.indexof(su

13、bstr) != -1) str = str.substring(index substr.length(); system.out.println(str); count system.out.println(count); 將取出的字段名轉(zhuǎn)化為變量名,例如: company_code= companycode company_c- companyc*/ public class changestr public static void main(stringargs) string str = company_code_name string newstr= new string(); s

14、tring substr = str.( _ for (int i = 0; i substr.length; i ) substri= substri.tolowercase(); if (i != 0) substri= substri.substring(0,1).touppercase() substri.substring(1); newstr =substri; system.out.println(newstr); 【辦法提煉】 1.字符串首字母大寫:str=str.substring(0,1).touppercase(); 2.去掉字符串的首字母:str=str.substri

15、ng(1); 3.以下劃線為界分割字符串并賦給一個(gè)字符串?dāng)?shù)組:string substr = str.split( _ 編寫一個(gè)辦法,返回一個(gè)double型二維數(shù)組,數(shù)組中的元素通過解析字符串參數(shù)獲得。如字符串參數(shù): 1,2;3,4,5;6,7,8 對應(yīng)的數(shù)組為: d0,0=1.0 d0,1=2.0 d1,0=3.0 d1,1=4.0 d1,2=5.0 d2,0=6.0 d2,1=7.0 d2,2=8.0 public class doublestr public static void main(stringargs) string str = 1,2;3,4,5;6,7,8 strings

16、ubstr = str.split( double d = new doublesubstr.length; for (int i = 0; i substr.length; i ) stringsubstr2 = substri.split( , di = new doublesubstr2.length; /要寫在for內(nèi)循環(huán)外面(難點(diǎn)) for (int j = 0; j substr2.length; j ) dij= double.parsedouble(substr2j); for (int i = 0; i d.length; i ) for (int j = 0; j di.l

17、ength; j ) system.out.print(dij system.out.println(); 【辦法提煉】 1. 把字符串轉(zhuǎn)換成數(shù) 類型(包裝類的靜態(tài)辦法): double d= double.parsedouble(str); 從輸入的一行字符串中求出其中最長的英文單詞的長度及其個(gè)數(shù),并輸出。單詞之間只能用空 隔開。 【運(yùn)行示例】 請輸入以空 隔開的字符串: nice tomeet you 字符串 = nice to meet you 最長單詞長度: 4 ,最長單詞個(gè)數(shù): 2 【編程提醒】輸入整行字符串,包括空 符,可以用法scanner類的nextline( )辦法。*/ i

18、mport java.util.scanner; public class test public static void main(stringargs) scanner sc = new scanner(system.in); system.out.println( 請輸入一行以空 隔開的字符串: string str =sc.nextline(); stringsubstr = str.split( int max = 0, count =0; for (int i = 0; i substr.length; i ) if (max substri.length() max =substri.length();

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論