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

首頁 > 編程 > Java > 正文

java基礎學習API之String類 六-1

2019-11-08 00:46:07
字體:
來源:轉載
供稿:網(wǎng)友

類 String

String 類代表字符串。java 程序中的所有字符串字面值(如 "abc" )都作為此類的實例實現(xiàn)。在 lang包下,所以不需要導包。API介紹:

一構造方法;

package wsj02;/** *  * @author Angus *  構造方法 *  String()  *  String(byte[] bytes)  *  String(byte[] bytes, int offset, int length)  *  String(char[] value)  *  String(char[] value, int offset, int count)  *  String(String original)  *   *  成員方法: *  	length() 長度 *   */public class StringDemo {	public static void main(String[] args) {		//方式一:String() 		String s1 = new String();		System.out.PRintln("s1:"+s1);//結果s1:     對象不是輸出的地址值,證明重寫了父類的方法。		System.out.println("s1.length:"+s1.length()); //s1.length:0   長度為零		  		//方式二 String(byte[] bytes) 		byte[] bys = {97,98,99,100,101};		String s2 = new String(bys);		System.out.println("s2:"+s2);//s2:abcde    編碼表		System.out.println("s2.length:"+s2.length()); //s2.length:5		  		//方式三 String(byte[] bytes, int offset, int length) 		String s3 = new String(bys, 2, 3);		System.out.println("s3:"+s3);//s3:cde		System.out.println("s3.length:"+s3.length()); //s3.length:3				//方式三  String(String original) 		String s4 = new String("abcd");		System.out.println("s4:"+s4);//s4:abcd		System.out.println("s4.length:"+s4.length()); //	s4.length:4		//優(yōu)化可以直接賦值		String s = "abcde";  	}}

疑問?

String s1 = new String();System.out.println("s1:"+s1);//結果s1:    和空     

對象new對象 輸出為什么不是地址值呢?

輸出不是地址值就是對string 的 toString 方法進行了重寫,跟進源碼可以發(fā)現(xiàn):

   /**     * Initializes a newly created {@code String} object so that it represents     * an empty character sequence.  Note that use of this constructor is     * unnecessary since Strings are immutable.     */    public String() {        this.value = "".value;    }

構造API如下圖:

疑問?

package wsj02;/** *  * @author Angus *	知識點1: 字符串一旦初始化就不可以被改變 *	知識點2; String s1 = new String("abc");和String s2 = "abc" 有區(qū)別嗎? */public class StringDemo3 {	public static void main(String[] args) {		//知識點1: 字符串一旦初始化就不可以被改變		String s = "hello";		s += "world";		System.out.println(s); //helloworld		//為什么輸出helloworld?		// 常量池值不可以被改變 但是引用s可以改變				//知識點2; String s1 = new String("abc");和String s2 = "abc" 有區(qū)別嗎?		//new 會在堆內(nèi)存中創(chuàng)建對象 然后再常量池中賦值  相當于兩個對象		//s2 直接在常量池中找對象,有創(chuàng)建沒有不創(chuàng)建。			}}

二String類的方法

部分方法操作:

package wsj02;/** * @author Angus *	booleane  equals(String str) //判斷字符串的內(nèi)容是否相同,區(qū)分大小寫 *	booleane  equalsIgnoreCase(String str) //判斷字符串的內(nèi)容是否相同,不考慮大小寫。 *	booleane  contains(String str)  //當且僅當此字符串包含指定的 char 值序列時,返回 true。 *	booleane  startsWith(String prefix)   //測試此字符串是否以指定的前綴開始。 *	booleane  endsWith(String suffix)  //測試此字符串是否以指定的后綴結束。 *	booleane  isEmpty() //當且僅當 length() 為 0 時返回 true。 */public class StringDemo4 {	public static void main(String[] args) {		String s = "HelloWorld";				//booleane  equals(String str) //判斷字符串的內(nèi)容是否相同,區(qū)分大小寫		System.out.println(s.equals("HelloWorld"));		System.out.println(s.equals("helloworld"));		System.out.println("----------------------");				//booleane  equalsIgnoreCase(String str) //判斷字符串的內(nèi)容是否相同,不考慮大小寫。		System.out.println(s.equalsIgnoreCase("HelloWorld"));		System.out.println(s.equalsIgnoreCase("helloworld"));		System.out.println("----------------------");				//booleane  contains(CharSequence s)  //當且僅當此字符串包含指定的 char 值序列時,返回 true。		System.out.println(s.contains("or"));		System.out.println(s.contains("ak"));		System.out.println("----------------------");				//booleane  startsWith(String prefix)   //測試此字符串是否以指定的前綴開始。		System.out.println(s.startsWith("H"));		System.out.println(s.startsWith("h"));		System.out.println("----------------------");				//booleane  endsWith(String suffix)  //測試此字符串是否以指定的后綴結束。		//......				//booleane  isEmpty() //當且僅當 length() 為 0 時返回 true。		System.out.println(s.isEmpty());		String s2 ="";		String s3 =null;		System.out.println(s2.isEmpty());		System.out.println(s3.isEmpty());		//Exception in thread "main" java.lang.NullPointerException		//at wsj02.StringDemo4.main(StringDemo4.java:44)		//空指針報錯,判斷是數(shù)據(jù)是否為空 null本身是空操作無意思。			}}

其它方法總結:

1.獲取    1.1 獲取字符串長度     int length();   1.2 根據(jù)位置獲取字符     char charAt(int index);   1.3 根據(jù)字符獲取在字符中的位置      int indexof(int ch) 返回的是ch在字符串中第一個出現(xiàn)的位置      int indexof(int ch,int FromIndex) 從fromIndex指定位置開始,獲取ch在字符串中出現(xiàn)的位置      int indexof(String str); 返回的是str在字符串中第一個出現(xiàn)的位置      int indexof(String str,int FromIndex) 從fromIndex指定位置開始,獲取str在字符串中出現(xiàn)的位置      反響索引一個字符出現(xiàn)的位置。      int lastindexof(int ch) 返回的是ch在字符串中第一個出現(xiàn)的位置      int lastindexof(int ch,int FromIndex) 從fromIndex指定位置開始,獲取ch在字符串中出現(xiàn)的位置      int lastindexof(String str); 返回的是str在字符串中第一個出現(xiàn)的位置      int lastindexof(String str,int FromIndex) 從fromIndex指定位置開始,獲取str在字符串中出現(xiàn)的位置    1.4獲取字符串中的一部分字符串,也叫子串。        String subString(int beginindex,intendindex) ;      String subString(int beginindex) 2.判斷   2.1兩個字符串是否相同      equals(Object obj)      equalsIgnoreCase(String str)   2.2字符串中是否包含某個字符串      contains(String str)   2.3兩個字符串是否以指定字符串開頭或結尾      boolean Startswith(String);      boolean endswith(String);   2.4字符串是否為空      boolean isEmpty();  3.字符串轉換      3.1將字符串變成字符串數(shù)組       String[] split(String regex);   3.2將字符串變成字符數(shù)組      char[] toCharArray();   3.1將字符串變成字節(jié)數(shù)組      byte[] getBytes();   3.4將字符串數(shù)組變成字符串      構造函數(shù) String(char[])              String(char[],offset,count)將字符數(shù)組中的一部分轉成字符串。       靜態(tài)函數(shù) static String copyValueof(char[])                static String copyValueof(char[],offset,count)將字符數(shù)組中的一部分轉成字符串。   3.5將字符串的字母大小寫轉換       String toUppercase();大寫       String toUppercase();小寫   3.6將字符串的內(nèi)容替換       String repalce(char oldch,char newch);       String repalce(String s1,String s2);   3.7將字符串兩端空格去掉       String trim();   3.8將字符串進行連接       String concat(String);4.比較  compareTo();小返回負數(shù) 等返回0 大返回正數(shù)

package wsj02;import java.util.Scanner;/** * @author Angus */public class StringDemo4 {	// 鍵盤錄入字符串 實現(xiàn)排序 大小寫轉換	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		System.out.println("請輸入英文字母:");		String s = sc.nextLine();		char[] ch = s.toCharArray();		System.out.println(ch);		for (int i = 0; i < ch.length; i++) { // 實現(xiàn)大小寫轉換 第一種方法			if (ch[i] >= 'a' && ch[i] <= 'z') {				ch[i] -= 32;			} else if (ch[i] >= 'A' && ch[i] <= 'Z') {				ch[i] += 32;			}		}		System.out.println(ch);		// 第二種		StringBuilder st = new StringBuilder();		for (int x = 0; x < ch.length; x++) {			if (ch[x] >= 'a' && ch[x] <= 'z') {				String ss = String.valueOf(ch[x]);// Character.toString				st.append(ss.toUpperCase());			} else {				String ss = String.valueOf(ch[x]);				st.append(ss.toLowerCase());			}		}		String s1 = new String(st);		System.out.println(s1);	}}

最后附上JDK使用文檔API 下載


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 玛多县| 安福县| 武汉市| 衡阳市| 泰顺县| 儋州市| 仲巴县| 万载县| 南昌县| 双城市| 仙桃市| 如东县| 金塔县| 巴彦淖尔市| 桐庐县| 德格县| 双江| 江阴市| 蒙阴县| 常宁市| 茂名市| 富蕴县| 平湖市| 乐东| 宜良县| 武隆县| 绍兴县| 涞源县| 随州市| 沙雅县| 化州市| 聂拉木县| 镇平县| 专栏| 犍为县| 博湖县| 天柱县| 武胜县| 乐昌市| 新乡县| 建昌县|