天天看點

Lesson_for_java_day12--java的常用類——String類

一、基本文法:

---------------------String類------------------------
String類:
	字元串是一個特殊的對象。
	字元串一旦初始化就不可以被改變。(字元串最大特點)
	
String str1 = "abc" 與String str2 = new String("abc")有什麼差別???
	String str1 = "abc";str1是一個類類型變量,"abc"是一個對象。
	String str2 = new String("abc");String類複寫了object類中的equals方法,
		該方法用于判斷字元串是否相同。
	str1和str2差別:
		str1在記憶體中有一個對象。str2在記憶體中有兩個對象。
	
	String str3 = "abc"  問:
		str1==str2.-----false   不同對象。
		str1==str3.-----true   字元串已經在記憶體中常量池記憶體在,str3初始化時發現已經
			存在,就不再獨立開辟空間建立字元串。
			
String類适用于描述字元串事物。提供了多個方法對字元串進行操作。
常見的操作有哪些?
	1、擷取:
		1.1字元串中包含的字元數,也就是字元串的長度。int length();
			(數組的length是屬性,字元串length();是方法)
		1.2根據位置擷取位置上的某個字元。char CharAt(int index);
			當通路到字元串中不存在的角标時會發生StringIndexOfBoundsException。
		1.3根據字元擷取該字元在字元串中的位置。
			int indexOf(int ch);傳回的是ch在字元串中第一次出現的位置。
			int indexOf(int ch,int fromIndex);從fromIndex指定位置開始,擷取ch在字元串中出現的位置。
				如果沒有找到,傳回值為-1。
			int indexOf(String str);傳回的是str在字元串中第一次出現的位置。
			int indexOf(String str,int fromIndex);從fromIndex指定位置開始,擷取str在字元串中出現的位置。
			
			int lastIndexOf(int ch);反向索引。(從右往左查詢,但是角标不變)
			int lastIndexOf(int ch, int fromIndex);
			int lastIndexOf(String str);
			int lastIndexOf(String str,int fromIndex);
		
	2、判斷:
		2.1字元串中是否包含某一個字元串。
			boolean contains(String str);
			特殊之處:indexOf(String str):可以索引str第一次出現位置,如果傳回-1,表示該
				str不在字元串中存在。是以,也可以用于判斷對指定内容是否包含。
				if(str.indexOf("aa"!=-1));
				該方法既可以判斷,又可以擷取出現的位置。
		2.2字元中是否有内容。
			boolean isEmpty();原理就是判斷長度是否為零。
		2.3字元串是否是以指定内容開頭。
			boolean startWith(String str);
		2.4字元串是否是以指定内容結尾。
			boolean endsWith(String str);
		2.5判斷字元串内容是否相同。(複寫了object中的equals方法)
			boolean equals(String str);
		2.6判斷内容是否相同,并忽略大小寫。
			boolean equalsIgnoreCase();
	3、類型轉換:
		3.1将字元數組轉換成字元串
			構造函數:String(char[])
					  String(char[],offset,count);将字元數組中的一部分轉換成字元串
			靜态方法:static String copyValueOf(char[]);
					  static String copyValueOf(char[] data,int offset,int count);
					  
					  static String ValueOf(char[]);
			
		3.2将字元串轉換成字元數組(****重點******)
			char[] toCharArray();
		3.3将位元組數組轉換成字元串
			String(byte[])
			String(byte[],offset,count);将位元組數組中的一部分轉
		3.4将字元串轉換成位元組數組
			byte[] getBytes();
		3.5将基本資料類型轉換成字元串
			static String ValueOf(double);
			static String valueOf(int);	
			eg:3+"";  == String.value(3);
		特殊:字元串和位元組數組在轉換過程中,是可以指定編碼表的
		
	4、替換:
		String replace(char oldChar,char newChar);
			(替換後産生新字元串,因為字元串初始化後就不會改變。如果替換不存在,傳回的是原串)
		
	5、切割:
		String[] split(regex);
		
	6、子串:擷取字元串中的一部分。
		String substring(begin);---從指定位置開始到結尾。
		String substring(begin,end); ---包含頭,不包含尾。
			如果角标不存在,會出現字元串角标越界異常。
			
	7、大小寫轉換、去除空格、比較:
		7.1将字元串轉換成大寫或者小寫
			String toUpperCase();
			String toLowerCase();	
		7.2将字元串兩端的多餘空格去除
			String trim();
		7.3對兩個字元串進行自然順序的比較
			int compareTo(String); 傳回值:相同為0;大于為正數;小于為負數。
			
	
           

代碼示例一:

public class TestString {

	public static void main(String[] args) {
		String str = "123 abc 456abcrrrr";
		String s1 = "abc";
		String s2 = "Abc";
		
		System.out.println("字元串長度:" + s1.length());
		System.out.println("傳回第1個位置的字元:" + s1.charAt(0));
		System.out.println("查找字元q在字元串中出現的位置:" + str.indexOf('q'));
		System.out.println("查找字元6在字元串中出現的位置:" + str.indexOf('6'));
		System.out.println("查找子字元串 abc 在字元串中出現的位置:" + str.indexOf(s1));
		System.out.println("查找子字元串 abc 在字元串中出現的位置:" + str.indexOf(s1,7));
		System.out.println("判斷兩個字元串是否相等:" + s1.equals(s2)); //false
		System.out.println("判斷兩個字元串是否相等(忽略大小寫):" + s1.equalsIgnoreCase(s2)); //true
		
		String s3 = "Hellh Whrld.";//不可變的常量
		System.out.println("把字元串中h 替換成 o : " + s3.replace('h', 'o'));
		System.out.println("s3: " + s3);
		s3 = s3.replace('h', 'o');//重新指派
		
		String s4 = "abcderdfabcgdfaaaaa";
		System.out.println("把字元串中abc 替換成 111 : " + s4.replaceAll("abc", "111"));
		
		String s5 = "kari.zhang";
		System.out.println("判斷字元串是否以k開頭: " + s5.startsWith("k"));//true
		System.out.println("判斷字元串是否以k結尾: " + s5.endsWith("k"));//false
		
		String s6 = "aBcDEfg1234";
		System.out.println("把字元串中小寫字母轉成大寫:" + s6.toUpperCase());
		System.out.println("把字元串中大寫字母轉成小寫:" + s6.toLowerCase());
		
		String s7 = "123456789abcdefgh";
		System.out.println("字元串截取: " + s7.substring(6));
		System.out.println("字元串截取: " + s7.substring(6, 12));//789abc
		
		String s8 = "    abcd    fdfdf    ";//trim, 去掉頭尾空格
		System.out.println("s8: " + s8);
		System.out.println("去掉頭尾空格:" + s8.trim());

		
	}
}
           

代碼示例二:

public class TestString2 {

	public static void main(String[] args) {
		
		String str = "a,b,c;d,e,g";
		String[] s1 = str.split(";");//String[] s10 = new String[10];
		for(int i=0; i<s1.length; i++) {
			System.out.println(s1[i]);
		}
		
		
		int i1 = 10;
		boolean b = true;
//		String s2 = i1;
		String s2 = String.valueOf(i1);
		String s3 = String.valueOf(b);
		
		System.out.println("s2:" + s2);
		
		String s4 = 10 + "";
	}
}
           

代碼示例三:

public class TestString3 {

	public static void main(String[] args) {
		String str = "abdfdfer";
		System.out.println("=====字元串轉位元組數組=====");
		byte[] b = str.getBytes();
		for(int i=0; i<b.length; i++) {
			System.out.println(b[i]);
		}
//		System.out.println((int)'a');
		System.out.println("=====字元串轉字元數組=====");
		char[] c = str.toCharArray();
		for(int i=0; i<c.length; i++) {
			System.out.println(c[i]);
		}
	}
}