第16章 字符串操作_第1頁
第16章 字符串操作_第2頁
第16章 字符串操作_第3頁
第16章 字符串操作_第4頁
第16章 字符串操作_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

字符串操作第十六章什么是程序的異常怎樣進行異常處理try…catch語句的使用回顧本章目標掌握String的定義String類常用的方法求字符串的長度字符串的比較字符串的查找字符串的截取String類Java中,字符串是String類的對象;可以通過使用String類提供的方法來完成對字符串的操作;創(chuàng)建一個字符串對象之后,將不能更改構(gòu)成字符串的字符;每當更改了字符串版本時,就創(chuàng)建了一個新的字符串對象,并在其內(nèi)包含所做的修改,原始字符串保持不變。String類的構(gòu)造方法String類的構(gòu)造方法共有13種重載方式,以下是常用的幾個:構(gòu)造方法說明String()將創(chuàng)建一個空字符串String(Stringoriginal)將新建一個字符串作為指定字符串的副本String(char[]value)將根據(jù)字符數(shù)組構(gòu)造一個新字符串String(byte[]bytes)將通過轉(zhuǎn)換指定的字節(jié)數(shù)組新建一個字符串String類構(gòu)造方法示例public

classStringDemo{

public

static

voidmain(String[]args){

char[]aryChar={‘N',‘E',‘W',‘E‘,’R’};Stringstr1="NEWER";

//利用一個字符串常量值創(chuàng)建新的字符串

Stringstr2=newString(“AMBOW");

//利用一個字符型數(shù)組創(chuàng)建新的字符串

Stringstr3=newString(aryChar);

System.out.println(str1);System.out.println(str2);System.out.println(str3);}}字符串長度String類中提供length成員方法,用來獲得字符串的長度,方法原型:intlength()該方法返回字符串中有效字符的個數(shù)。public

classStringDemo{

public

static

voidmain(String[]args){Stringstr1="JohnSmith";Stringstr2=newString("ILoveJava");System.out.println(str1.length());System.out.println(str2.length());}}字符串比較要判斷兩個字符串是否相等,可以使用“==”運算符和equals()方法,但是得到的結(jié)果可能不完全相同;==運算符用于比較兩個引用是否指向同一個對象;而equals()方法則是比較兩個字符串中的內(nèi)容是否相同,其原型:

booleanequals(ObjectanObject)

如果相等返回true,否則返回false。字符串比較示例public

classStringDemo{

public

static

voidmain(String[]args){Stringstr1=“AMBOW",str2="AMBOW";Stringstr3=newString("NEWER"),str4=newString("NEWER");

if(str1==str2){System.out.println("str1和str2指向同一字符串");}

else{System.out.println("str1和str2分別指向不同字符串");}

if(str1.equals(str2)){System.out.println("str1和str2的內(nèi)容完全相同");}

else{System.out.println("str1和str2的內(nèi)容不相同");}

if(str3==str4){System.out.println("str3和str4指向同一字符串");}

else{System.out.println("str3和str4分別指向不同字符串");}

if(str3.equals(str4)){System.out.println("str3和str4的內(nèi)容完全相同");}

else{System.out.println("str3和str4的內(nèi)容不相同");}}}其它的比較方法方法原型說明booleanequalsIgnoreCase(StringanotherString)判斷字符串a(chǎn)notherString是否與當前字符串相等,忽略大小寫形式intcompareTo(StringanotherString)根據(jù)ASCII碼比較字符串a(chǎn)noterString和當前字符串的大小,比較方式類似于C語言中的strcmp函數(shù)booleanstartsWith(Stringprefix)判斷當前字符串是否以字符串prefix為開頭booleanendsWith(Stringsuffix)判斷當前字符串是否以字符串suffix為后綴字符串搜索如果需要搜索某個字符(或某個子串)在字符串中是否出現(xiàn)過,這就要使用到indexOf方法和lastIndexOf方法。方法原型說明intindexOf(intch)搜索字符ch在當前字符串中第一次出現(xiàn)的索引,沒有出現(xiàn)則返回-1intindexOf(Stringstr)搜索字符串str在當前字符串中第一次出現(xiàn)的索引,沒有出現(xiàn)則返回-1intlastIndexOf(intch)搜索字符ch在當前字符串中最后一次出現(xiàn)的索引,沒有出現(xiàn)則返回-1intlastIndexOf(Stringstr)搜索字符串str在當前字符串中最后一次出現(xiàn)的索引,沒有出現(xiàn)則返回-1字符串搜索示例public

classStringDemo{

public

static

voidmain(String[]args){StringstrEmail="java@";

intindex;

System.out.println("E-mail地址:"+strEmail);index=strEmail.indexOf('@');System.out.println("@字符出現(xiàn)的索引:"+index);index=strEmail.indexOf("sun");System.out.println("字符串\"sun\"出現(xiàn)的索引:"+index);index=strEmail.lastIndexOf('a');System.out.println("a字符最后一次出現(xiàn)的索引:"+index);}}提取字符串方法原型說明charcharAt(intindex)用于從指定位置提取單個字符,該位置由index指定,索引值必須為非負Stringsubstring(intindex)用于提取從index指定的位置開始的字符串部分Stringsubstring(intbegin,intend)用于提取begin和end位置之間的字符串部分Stringconcat(Stringstr)用于連接兩個字符串,并新建一個包含調(diào)用字符串的字符串對象Stringreplace(charoldChar,charnewChar)用于將調(diào)用字符串中出現(xiàn)oldChar指定的字符全部都替換為newChar指定的字符replaceAll(String

regex,Stringreplacement)用于將調(diào)用字符串中出現(xiàn)或者匹配regex的字符串全部都替換為replacement指定的字符Stringtrim()用于返回一個前后不含任何空格的調(diào)用字符串的副本提取字符串示例public

classStringDemo{

public

static

voidmain(String[]args){Stringstr1="JavaisOOP";Stringstr2=newString(“newer");

System.out.println(str1.charAt(2));System.out.println(str1.substring(5));System.out.println(str1.substring(2,9));System.out.println(str1.concat(str2));System.out.println(str1+str2);System.out.println(str1.replace(‘e',‘o'));System.out.println(str1.trim());}}更改字符串的大小寫形式有時候,我們需要將字符串中字符的大小寫形式進行轉(zhuǎn)換。方法原型說明StringtoUpperCase()返回當前字符串的全大寫形式StringtoLowerCase()返回當前字符串的全小寫形式更改大小寫形式示例public

classStringDemo{

public

static

voidmain(String[]args){Stringstr1="JavaisOOP";Stringstr2;

str2=str1.toLowerCase();System.out.println(str2);

str2=str1.toUpperCase();System.out.println(str2);}}數(shù)據(jù)格式轉(zhuǎn)化在某些特定的場合,我們可能需要將字符串轉(zhuǎn)化成其它格式的數(shù)據(jù)進行操作;方法原

溫馨提示

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

最新文檔

評論

0/150

提交評論