国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

字符串之String類

2019-11-14 23:51:13
字體:
來源:轉載
供稿:網友
字符串之String類

主要涉及在程序運行初始化之后不能改變的字符串類String和字符串內容可以動態改變的類StringBuffer,以及用于字符串轉換詞法分析類StringTokenizer;同時還將介紹字符串和其他數據類型之間的相互轉換。

java中將字符串作為對象來處理,在對象中封裝了一系列方法來進行字符串處理。

String類

String位于java.lang包中,因此在程序中不需要使用import語句就可以用String來實例化對象。String類主要用來處理程序初始化后不能改變的字符串。

字符串的構造

字符串常量是用雙引號分隔的一系列java合法字符。在用賦值運算符進行字符串初始化時,JVM自動為每個字符串生成一個String類實例。字符串聲明格式如下:

String s;

創建字符串可以使用String類的構造方法。

s=new String("xjtu");

也可以寫成:

s="xjtu";

聲明和實例化對象可以一步完成。

利用String類提供的構造方法,可以生成空的字符串或者其他基本數據類型生成字符串對象。

(1)利用下面的方法可以生成空的String實例。

String str=new String();

(2)在String類提供的構造方法中,可以由字符數組、字節數組以及字符串緩沖區來構造字符串,如下所示:

char c1[]={'1','2','3','4'};

char c2[]={'1','2','3','4','5'};

String str1=new String(c1);

String str2=new String(c2,0,3);

System.out.

(3)利用字節數組生成字符串:

byte c1[]={66,67,68};

byte c2[]={65,66,67,68};

String str1=new String(c1);

String str2=new String(c2,1,3);

System.out.print(str1);

System.out.print(str1);

結果均是BCD

String類的常用方法

String類提供了length(),charAt(),indexOf(),lastIndexOf(),getChars(),getBytes(),toCharArray()等方法。這些方法中按照用途分為字符串長度計算、字符串比較、字符串檢索、字符串的截取、替換等方法。

字符串長度計算

使用String類中的方法length()方法可以獲取一個字符串的長度。定義如下:

public int length()

該方法返回字符串的16-bit的Unicode字符的數量。例如:

String s="xjtu";

s.length()為4

字符串比較

字符串比較的方法有equals()、equalsIgnoreCase()、startsWith()、endsWith()、regionMatches()、compareTo()、compareToIgnoreCase()等方法。

1)equals()和equalsIgnoreCase()方法

equals()定義:

public boolean equals(String s)

該方法用于比較當前字符串對象的實體是否與參數指定的字符串s的實體相同。注意String類中的equals()方法和"=="的區別。

equalsIgnoreCase()定義:

public booleanequalsIgnoreCase(String s)

字符串對象調用調用equalsIgnoreCase(String s)來比較當前對象是否與參數指定的字符串s相同,比較時忽略大小寫。

2)startsWith()和endsWith()方法

字符串對象調用startsWith()和endsWith()方法分別判斷當前字符串對象的前綴和后綴是否是參數指定的字符串s。

定義:

public booleanstartsWith(String s)和public boolean endsWith(String s);

例如:

String str1="20150418";

String str2="20140418";

str1.startsWith("2015")的值是true;

str2.endsWith("0418")的值是true;

3)regionMatches()方法

聲明格式如下:

public booleanregionMatches(int firstStart,String other,int otherStart,int length)和public booleanregionMatches(boolean b,int firstStart,String other,int otherStart,int length)

從當前指定的字符串參數firstStart指定的位置開始處,取長度為length的一個子串,并將這個子串與參數other指定的一個子串進行比較。其中other指定的子串是是從參數otherStart指定的位置開始,從other中取長度為length的一個子串。如果兩個子串相同就返回true,否則false。

4)compareTo()和compareToIgnoreCase()方法

聲明格式:

public intcompareTo(String s)

public intcompareToIgnoreCase(String s)

compareTo()方法按照字典順序與參數s指定的字符串比較大小。如果當前字符串與s相同則返回0;如果如果大于s則返回正值,小于s返回負值。注意小寫字母大于大寫字母,而compareToIgnoreCase()方法忽略大小寫。

字符串檢索

搜索指定字符或字符串在字符串中出現的位置,用于字符或字符串在字符串中的定位。方法聲明格式如下:

public int indexOf(int ch)

public int indexOf(int ch,int fromIndex)

public int indexOf(String str)

public int indexOf(String str,int fromIndex)

上述四個重載方法分別用于在字符串中定位指定的字符和字符串,并且在方法種可以通過fromIndex來指定匹配的起始地址。如果沒有檢索到字符或字符串,該方法返回值是-1。

String s="xjtuxjtu";

int i;

i=s.indexOf('x');//0

i=s.indexOf("xjtu",0);//0

i=s.indexOf('xjtu',4);//4

另外,String類還提供字符串中的最后位置的定位,方法聲明格式如下:

public int lastIndexOf(int ch)

public int lastIndexOf(int ch,int fromIndex)

public int lastIndexOf(String str)

public int lastIndexOf(String str,int fromIndex)

上述4個重載方法分別用于在字符串中定位定位指定的字符和字符串最后出現的位置,并且在上述方法中可以通過fromIndex來指定匹配的起始位置。如果沒有檢索到字符或字符串,該方法返回值是-1。

字符串截取

在字符串中截取子字符串,其聲明格式:

public String substring(int beginIndex)

該方法將獲得一個當前字符串的子串,該子串是從當前字符串的beginIndex處截取到最后所得到的字符串。

public String substring(int beginIndex,int endIndex)

該方法將獲得一個當前字符串的子串,該子串是從當前字符串的beginIndex處截取到endIndex-1結束處所得到的字符串。

字符串的替換

public String replace(char oldChar,char newChar)

字符串對象s調用該方法可以獲得一個串對象,這個串對象是用參數newChar指定的字符替換s中的oldChar指定對所有的字符而得到的字符串。

public String replace(String old,String new)

字符串對象s調用該方法可以獲得一個串對象,這個串對象是用參數new指定的字符串替換s中的old指定對所有的字符串而得到的字符串。

String s="xjtuxjtu";System.out.println(s.replace('x','s'));//sjtusjtu

System.out.println(s.replace("xjtu","Tsinghua"));//TsinghuaTsinghua

public String trim()

一個字符串s通過調用方法trim()得到一個字符串對象,該字符串對象是s去掉前后空格后的字符串。

其他的一些方法

1)字符串大小寫轉換

聲明格式如下:

public String toUpperCase(Local local)//僅對指定位置轉換

public String toUpperCase()

public String toLowerCase(Local local)// 僅對指定位置轉換

public String toLowerCase()

2)轉換為字符串數組

聲明格式如下:

public char[] toCharArray()

該方法用于將字符串轉換為字符串數組。該方法的返回值類型為字符數組,如下

String str=new String("I love xjtu");

char[] ch;

ch=str.toCharArray();

System.out.print(ch);//I love xjtu

3)字符串到字符數組之間的轉換

聲明格式:

getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)

該方法用于將字符串中的字符內容復制到字符數組中。其中srcBegin為復制的起始位置,srcEnd-1為復制的終止位置、字符串數值dst為目的字符數組,dstBegin為目的字符數組的復制的起始位置。復制的時候會覆蓋原來位置數據。

4)連接兩個字符串

聲明格式如下:

public String concat(String str)

該方法用于將兩個字符串連接在一起,與字符串的"+" 操作符功能相同。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建阳市| 扎赉特旗| 阳朔县| 朝阳市| 新田县| 海南省| 余江县| 沾化县| 平阴县| 南宁市| 招远市| 康平县| 洛南县| 信丰县| 会理县| 普格县| 长寿区| 大洼县| 南江县| 北碚区| 周宁县| 平陆县| 建阳市| 南安市| 社旗县| 贺州市| 从化市| 曲水县| 九江市| 津市市| 青川县| 邵阳县| 瑞丽市| 平罗县| 治多县| 五家渠市| 皮山县| 密云县| 宣化县| 衡南县| 香河县|