字符串的操作


字符串操作


1.獲取子字符串

通過String類的substring()方法可對字符串進行截取。有以下兩種方法:

1.substring(int beginIndex)

該方法返回的是從指定的索引位置開始截取直到該字符串結尾的子串。
語法:

str.substring(int beginIndex)

String str = "Hello World";         //定義字符串str
String substr = str.substring(3);   //獲取字符串,此時substr值為lo World

在字符串中,空格占用一個索引位置
字符串下標是從0開始的

2.substring(int beginIndex,int endIndex)

該方法返回的是從字符串某一索引位置開始截取至某一索引位置結束的子串。
語法:

str.substring(int beginIndex,int endIndex)

beginIndex:開始截取子字符串的索引位置。
endIndex:子字符串在整個字符串中的結束位置(截取時不包括它)
例:

public class Subs { // 創建類
	public static void main(String args[]) { // 主方法
		String str = "hello world"; // 定義的字符串
		String substr = str.substring(0, 3); // 對字符串進行截取
		System.out.println(substr); // 輸出截取后的字符串
	}
}

運行結果:
hel

2.字符串替換

replace()方法可實現將指定的字符或字符串替換成新的字符或字符串
語法:

str.replace(char oldChar,char newChar)

oldChar:要替換的字符或字符串
newChar:用於替換原來字符串的內容
replace()方法返回的結果是一個新的字符串。如果字符串oldChar沒有出現在該對象表達式中的字符串序列中,則將原字符串返回。
例:

public class NewStr { // 創建類
	public static void main(String args[]) { // 主方法
		String str = "address"; // 定義字符串str
		 // 字符串str中的字符"a"替換成"A"后返回的新字符串newstr
		String newstr = str.replace("a", "A");
		System.out.println(newstr); // 將字符串newstr輸出
	}
}

運行結果:
Address
如果要替換的字符oldChar在字符串中重復多次出現,replace()方法會將所有oldChar全部替換成newChar。

3.去除空格

trim()方法返回字符串的副本,去除前導空格和尾部空格。
語法:

str.trim()

例:

public class Blak { // 創建類
	public static void main(String args[]) { // 主方法
		String str = "  Java  class   "; // 定義字符串str
		System.out.println("字符串原來的長度:" + str.length()); // 將str原來的長度輸出
		System.out.println("去掉空格后的長度:" + str.trim().length());
		// 將str去掉前導和尾部的空格后的結果輸出
	}
}

運行結果:
字符串原來的長度:16
去掉空格后的長度:11

另:用replace()也可實現去除空格的操作。

4.判斷字符的開始與結尾

startWith()方法與endsWith()方法分別用於判斷字符串是否以指定的內容開始或結束。這兩個方法的返回值都為boolean類型。

1.startWith()方法

該方法用於判斷當前字符串對象的前綴是否是參數指定的字符串。
語法:

str.startWith(String prefix)

prefix指作為前綴的字符串

2.endsWith()方法

該方法用於判斷當前字符串對象的前綴是否是參數指定的字符串。
語法:

str.endsWith(String suffix)

suffix指作為后綴的字符串

3.實例

public class StartOrEnd { // 創建類
	public static void main(String args[]) { // 主方法
		String num1 = "22045612"; // 定義字符串num1
		String num2 = "21304578"; // 定義字符串num2
		boolean b = num1.startsWith("22"); // 判斷字符串num1是否以'22'開頭
		boolean b2 = num1.endsWith("78"); // 判斷字符串num1是否以'78'開頭
		boolean b3 = num2.startsWith("22"); // 判斷字符串num2是否以'22'開頭
		boolean b4 = num2.endsWith("78"); // 判斷字符串num2是否以'78'開頭
		System.out.println("字符串num1是以'22'開始的嗎?" + b);
		System.out.println("字符串num1是以'78'結束的嗎?" + b2); // 輸出信息
		System.out.println("字符串num2是以'22'開始的嗎?" + b3);
		System.out.println("字符串num2是以'78'結束的嗎?" + b4);
	}
}

運行結果:
字符串num1是以'22'開始的嗎?true
字符串num1是以'78'結束的嗎?false
字符串num2是以'22'開始的嗎?false
字符串num2是以'78'結束的嗎?true

5.判斷字符串是否相等

對字符串對象進行比較不能簡單地使用比較運算符“==”,因為比較運算符比較的是兩個字符串的地址是否相同。即使兩個字符串的內容相同,兩個對象的內存地址也是不同的,使用比較運算符任然會返回false。
例:

String tom = new String("I am a student");
String jerry = new String("I am a student");
boolean b = (tom == jerry);

此時,b的值為false,因為字符串是對象,tom、jerry是引用。
因此,要比較兩個字符串內容是否相等,應使用equals()方法和equalIgnoreCase()方法。

1.equals()方法

如果兩個字符串具有相同的字符和長度,則使用equals()方法進行比較時,返回true。
語法:

str.equals(String otherstr)

其中,str、otherstr是參加比較的兩個字符串對象。

2.equalsIgnoreCase()方法

使用equals()方法對字符串進行比較時是區分大小寫的,而使用equalsIgnoreCase()方法是在忽略了大小寫的情況下比較兩個字符串是否相等,返回的結果仍為boolean類型。
語法:

str.equalsIgnoreCase(String otherstr)

其中,str、otherstr是參加比較的兩個字符串對象。

3.實例

public class Opinion { // 創建類
	public static void main(String args[]) { // 主方法
		String s1 = new String("abc"); // 創建字符串對象s1
		String s2 = new String("ABC"); // 創建字符串對象s2
		String s3 = new String("abc"); // 創建字符串對象s3
		boolean b = s1.equals(s2); // 使用equals()方法比較s1與s2
		// 使用equalsIgnoreCase()方法比較s1與s2
		boolean b2 = s1.equalsIgnoreCase(s2); 
		System.out.println(s1 + " equals " + s2 + " :" + b); // 輸出信息
		System.out.println(s1 + " equalsIgnoreCase " + s2 + " :" + b2);
	}
}

運行結果:
abc equals ABC :false
abc equalsIgnoreCase ABC :true

6.按字典順序比較兩個字符串

compareTo()方法為按字典順序比較兩個字符串。如果按字典順序此String對象位於參數字符串之前,則比較結果為一個負整數。如果按字典順序此String對象位於參數字符串之后,則比較結果為一個正整數。如果這兩個字符串相等,則結果為0。
語法:

str.compareTo(String otherstr)

其中,str、otherstr是參加比較的兩個字符串對象。

例:

public class Wordbook { // 創建類
	public static void main(String args[]) { // 主方法
		String str = new String("b");
		String str2 = new String("a"); // 用於比較的3個字符串
		String str3 = new String("c");
		System.out.println(str + " compareTo " + str2 + ":"
				+ str.compareTo(str2)); // 將str與str2比較的結果輸出
		System.out.println(str + " compareTo " + str3 + ":"
				+ str.compareTo(str3)); // 將str與str3比較的結果輸出
	}
}

運行結果:
b compareTo a:1
b compareTo c:-1

7.字母大小寫轉換

1.toLowerCase()方法

該方法將String轉換為小寫。如果字符串中沒有應該被轉換的字符,則將原字符串返回;否則將返回一個新的字符串。
語法:

str.toLowerCase()

其中,str是要進行轉換的字符串

2.toUpperCase()方法

該方法將String轉換為大寫。如果字符串中沒有應該被轉換的字符,則將原字符串返回;否則將返回一個新的字符串。
語法:

str.toUpperCase()

其中,str是要進行轉換的字符串

*使用toLowerCase()方法和toUpperCase()方法進行大小寫轉換時,數字或非字符不受影響。

3.實例

public class UpAndLower { // 創建類
	public static void main(String args[]) { // 主方法
		String str = new String("abc DEF"); // 創建的字符串str
		String newstr = str.toLowerCase(); //使用toLowerCase()方法實行小寫轉換
		String newstr2 = str.toUpperCase();//使用toUpperCase()方法實行大寫轉換
		System.out.println(newstr);// 將轉換后的結果輸出
		System.out.println(newstr2);
	}
}

運行結果:
abc def
ABC DEF

8.字符串分割

使用split()方法可以使字符串按指定的分割字符或字符串對內容進行分割,並將分割后的結果存放在字符串數組中。split()方法有以下兩種形式。

1.split(String sign)

該方法根據給定的分割符對字符串進行拆分。
語法:

str.split(String sign)

其中,sign為分割字符串的分割符。
如果想定義多個分割符,可使用符號“|”。例如“,|=”表示分割符分別為“,”和“=”。

2.split(String sign,int limit)

該方法可根據給定的分割符對字符串進行拆分,並限定拆分的次數。
語法:

str.split(String sign,int limit)

sign:分割字符串的分割符
limit:限制的分割字數

3.實例

public class Division { // 創建類
	public static void main(String args[]) { // 主方法
		String str = new String("abc,def,ghi,gkl"); // 定義的字符串str
		// 使用split()方法對字符串進行拆分,返回字符串數組
		String[] newstr = str.split(",");
		for (int i = 0; i < newstr.length; i++) { // 使用for循環遍歷字符數組
			System.out.println(newstr[i]); // 輸出信息
		}
		// 對字符串進行拆分,並限定拆分次數,返回字符數組
		String[] newstr2 = str.split(",", 2);
		for (int j = 0; j < newstr2.length; j++) { // 循環遍歷字符數組
			System.out.println(newstr2[j]); // 輸出信息
		}
	}
}

運行結果:
abc
def
ghi
gkl
abc
def,ghi,gkl


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM