六、字符串

–< java.lang >-- String字符串
java中用String類進行描述。對字符串進行了對象的封裝。這樣的好處是可以對字符串這種常見數據進行方便的操作。對象封裝後,可以定義N多屬性和行爲。

如何定義字符串對象呢?String s = “abc”;只要是雙引號引起的數據都是字符串對象。

特點:字符串一旦被初始化,就不可以被改變,存放在方法區中的常量池中。

String s1 = "abc"; // s1指向的內存中只有一個對象abc。
String s2 = new String("abc"); // s2指向的內容中有兩個對象abc、new 。

System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true ,字符串中equals比較的是字符串內容是否相同。

字符串的方法:

1:構造方法:將字節數組或者字符數組轉成字符串。

String s1 = new String();//創建了一個空內容的字符串。 
String s2 = null;//s2沒有任何對象指向,是一個null常量值。
String s3 = "";//s3指向一個具體的字符串對象,只不過這個字符串中沒有內容。
//一般在定義字符串時,不用new。
String s4 = new String("abc");
String s5 = "abc"; 一般用此寫法
new String(char[]);//將字符數組轉成字符串。
new String(char[],offset,count);//將字符數組中的一部分轉成字符串。

2:一般方法
按照面向對象的思想:
2.1 獲取:
2.1.1:獲取字符串的長度。length();
2.1.2:指定位置的字符。char charAt(int index);
2.1.3:獲取指定字符的位置。如果不存在返回-1,所以可以通過返回值-1來判斷某一個字符不存在的情況。

int indexOf(int ch);//返回第一次找到的字符角標
int indexOf(int ch,int fromIndex); //返回從指定位置開始第一次找到的角標
int indexOf(String str); //返回第一次找到的字符串角標
int indexOf(String str,int fromIndex);
int lastIndexOf(int ch);
int lastIndexOf(int ch,int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);

2.1.4:獲取子串。

String substring(int start);//從start位開始,到length()-1爲止.
String substring(int start,int end);//從start開始到end爲止。
//包含start位,不包含end位。
substring(0,str.length());//獲取整串

2.2 判斷:
2.2.1:字符串中包含指定的字符串嗎?
boolean contains(String substring);
2.2.2:字符串是否以指定字符串開頭啊?
boolean startsWith(string);
2.2.3:字符串是否以指定字符串結尾啊?
boolean endsWith(string);
2.2.4:判斷字符串是否相同
boolean equals(string);//覆蓋了Object中的方法,判斷字符串內容是否相同。
2.2.5:判斷字符串內容是否相同,忽略大小寫。
boolean equalsIgnoreCase(string) ;

2.3 轉換:
2.3.1:通過構造函數可以將字符數組或者字節數組轉成字符串。
2.3.2:可以通過字符串中的靜態方法,將字符數組轉成字符串。

static String copyValueOf(char[] );
static String copyValueOf(char[],int offset,int count);
static String valueOf(char[]);
static String valueOf(char[],int offset,int count);

2.3.3:將基本數據類型或者對象轉成字符串。

static String valueOf(char);
static String valueOf(boolean);
static String valueOf(double);
static String valueOf(float);
static String valueOf(int);
static String valueOf(long);
static String valueOf(Object);

2.3.4:將字符串轉成大小寫。

String toLowerCase();
String toUpperCase();

2.3.5:將字符串轉成數組。

char[] toCharArray();//轉成字符數組。
byte[] getBytes();//可以加入編碼表。轉成字節數組。

2.3.6:將字符串轉成字符串數組。切割方法。
String[] split(分割的規則-字符串);
2.3.7:將字符串進行內容替換。注意:修改後變成新字符串,並不是將原字符串直接修改。

String replace(oldChar,newChar);
String replace(oldstring,newstring);

2.3.8:

String concat(string); //對字符串進行追加。
String trim();//去除字符串兩端的空格
int compareTo();/*如果參數字符串等於此字符串,則返回值 0;如果此字符串按字典順序小於字符串參數,則返回一個小於 0 的值;如果此字符串按字典順序大於字符串參數,則返回一個大於 0 的值。*/

–< java.lang >-- StringBuffer字符串緩衝區
構造一個其中不帶字符的字符串緩衝區,初始容量爲 16 個字符。
特點:
1:可以對字符串內容進行修改。
2:是一個容器。
3:是可變長度的。
4:緩衝區中可以存儲任意類型的數據。
5:最終需要變成字符串。

容器通常具備一些固定的方法:
1,添加。

StringBuffer append(data); //在緩衝區中追加數據。追加到尾部。
StringBuffer insert(index,data); //在指定位置插入數據。

2,刪除。

StringBuffer delete(start,end); //刪除從start至end-1範圍的元素
StringBuffer deleteCharAt(index); //刪除指定位置的元素
//sb.delete(0,sb.length()); //清空緩衝區。

3,修改。

StringBuffer replace(start,end,string); //將start至end-1替換成string
void setCharAt(index,char); //替換指定位置的字符
void setLength(len); //將原字符串置爲指定長度的字符串

4,查找。(查不到返回-1)

int indexOf(string); //返回指定子字符串在此字符串中第一次出現處的索引。
int indexOf(string,int fromIndex);//從指定位置開始查找字符串
int lastIndexOf(string); //返回指定子字符串在此字符串中最右邊出現處的索引。
int lastIndexOf(string,int fromIndex); //從指定的索引開始反向搜索

5,獲取子串。

string substring(start); //返回start到結尾的子串
string substring(start,end); //返回start至end-1的子串

6,反轉。
StringBuffer reverse(); //字符串反轉

–< java.lang >-- StringBuilder字符串緩衝區
JDK1.5出現StringBuiler;構造一個其中不帶字符的字符串生成器,初始容量爲 16 個字符。該類被設計用作 StringBuffer 的一個簡易替換,用在字符串緩衝區被單個線程使用的時候(這種情況很普遍)。
方法和StringBuffer一樣;

StringBuffer 和 StringBuilder 的區別:
StringBuffer線程安全。
StringBuilder線程不安全。

單線程操作,使用StringBuilder 效率高。
多線程操作,使用StringBuffer 安全。

		StringBuilder sb = new StringBuilder("abcdefg");
		sb.append("ak");  //abcdefgak
		sb.insert(1,"et");//aetbcdefg
		sb.deleteCharAt(2);//abdefg
		sb.delete(2,4);//abefg
		sb.setLength(4);//abcd
		sb.setCharAt(0,'k');//kbcdefg
		sb.replace(0,2,"hhhh");//hhhhcdefg
		
//想要使用緩衝區,先要建立對象。
		StringBuffer sb = new StringBuffer();		
		sb.append(12).append("haha");//方法調用鏈。
		String s = "abc"+4+'q';
		s = new StringBuffer().append("abc").append(4).append('q').toString();

class  Test{
	public static void main(String[] args) {
		String s1 = "java";
		String s2 = "hello";
		method_1(s1,s2);
		System.out.println(s1+"...."+s2); //java....hello
		
		StringBuilder s11 = new StringBuilder("java");
		StringBuilder s22 = new StringBuilder("hello");
		method_2(s11,s22);
		System.out.println(s11+"-----"+s22); //javahello-----hello
	}
	public static void method_1(String s1,String s2){
		s1.replace('a','k');
		s1 = s2;
	}
	public static void method_2(StringBuilder s1,StringBuilder s2){
		s1.append(s2);
		s1 = s2;
	}
}
發佈了44 篇原創文章 · 獲贊 89 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章