String構造方法javascript
package cn.itcast_01; /* * 字符串:就是由多個字符組成的一串數據。也能夠當作是一個字符數組。 * 經過查看API,咱們能夠知道 * A:字符串字面值"abc"也能夠當作是一個字符串對象。 * B:字符串是常量,一旦被賦值,就不能被改變。 * * 構造方法: * public String():空構造 * public String(String original):把字符串常量值轉成字符串 * * 字符串的方法: * public int length():返回此字符串的長度。 */ public class StringDemo { public static void main(String[] args) { // public String():空構造 String s1 = new String(); System.out.println("s1:" + s1); System.out.println("s1.length():" + s1.length()); System.out.println("--------------------------"); //s1: //s1.length():0 //public String(String original):把字符串常量值轉成字符串 String s6 = new String("abcde"); System.out.println("s6:" + s6); System.out.println("s6.length():" + s6.length()); System.out.println("--------------------------"); //字符串字面值"abc"也能夠當作是一個字符串對象。 String s7 = "abcde"; System.out.println("s7:"+s7); System.out.println("s7.length():"+s7.length()); } }
字符串的特色:一旦被賦值,就不能改變。java
可是引用能夠改變編程
package cn.itcast_02; /* * 字符串的特色:一旦被賦值,就不能改變。 */ public class StringDemo { public static void main(String[] args) { String s = "hello"; s += "world"; System.out.println("s:" + s); // helloworld } }
圖解:數組
String s = new String(「hello」)和 String s = 「hello」;的區別?post
String字面值對象和構造方法建立對象的區別優化
package cn.itcast_02; /* * String s = new String(「hello」)和String s = 「hello」;的區別? * 有。前者會建立2個對象,後者建立1個對象。 * * ==:比較引用類型比較的是地址值是否相同 * equals:比較引用類型默認也是比較地址值是否相同,而String類重寫了equals()方法,比較的是內容是否相同。 */ public class StringDemo2 { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; System.out.println(s1 == s2);// false System.out.println(s1.equals(s2));// true } }
圖解:this
String s5 = "hello"; String s6 = "hello"; System.out.println(s5 == s6);// 字符串字面量,直接從內存找,因此true System.out.println(s5.equals(s6));// true
package cn.itcast_02; /* * 看程序寫結果 * 字符串若是是變量相加,先開空間,在拼接。 * 字符串若是是常量相加,是先加,而後在常量池找,若是有就直接返回,不然,就建立。 */ public class StringDemo4 { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; String s3 = "helloworld"; System.out.println(s3 == s1 + s2);// false。字符串若是是變量相加,先開空間,再拼接。 System.out.println(s3.equals((s1 + s2)));// true System.out.println(s3 == "hello" + "world");//true。字符串若是是常量相加,是先加,而後在常量池找,若是有就直接返回,不然,就建立。 System.out.println(s3.equals("hello" + "world"));// true // 經過反編譯看源碼,咱們知道這裏已經作好了處理。 // System.out.println(s3 == "helloworld"); // System.out.println(s3.equals("helloworld")); } }
String類的判斷功能:spa
package cn.itcast_03; /* * String類的判斷功能: * boolean equals(Object obj):比較字符串的內容是否相同,區分大小寫 * boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫 * boolean contains(String str):判斷大字符串中是否包含小字符串 * boolean startsWith(String str):判斷字符串是否以某個指定的字符串開頭 * boolean endsWith(String str):判斷字符串是否以某個指定的字符串結尾 * boolean isEmpty():判斷字符串是否爲空。 * * 注意: * 字符串內容爲空和字符串對象爲空。 * String s = "";//對象存在,因此能夠調方法 * String s = null;//對象不存在,不能調方法 */ public class StringDemo { public static void main(String[] args) { // 建立字符串對象 String s1 = "helloworld"; String s2 = "helloworld"; String s3 = "HelloWorld"; // boolean equals(Object obj):比較字符串的內容是否相同,區分大小寫 System.out.println("equals:" + s1.equals(s2)); System.out.println("equals:" + s1.equals(s3)); System.out.println("-----------------------"); // boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫 System.out.println("equals:" + s1.equalsIgnoreCase(s2)); System.out.println("equals:" + s1.equalsIgnoreCase(s3)); System.out.println("-----------------------"); // boolean contains(String str):判斷大字符串中是否包含小字符串 System.out.println("contains:" + s1.contains("hello")); System.out.println("contains:" + s1.contains("hw")); System.out.println("-----------------------"); // boolean startsWith(String str):判斷字符串是否以某個指定的字符串開頭 System.out.println("startsWith:" + s1.startsWith("h")); System.out.println("startsWith:" + s1.startsWith("hello")); System.out.println("startsWith:" + s1.startsWith("world")); System.out.println("-----------------------"); // 練習:boolean endsWith(String str):判斷字符串是否以某個指定的字符串結尾這個本身玩 // boolean isEmpty():判斷字符串是否爲空。 System.out.println("isEmpty:" + s1.isEmpty()); String s4 = ""; String s5 = null; System.out.println("isEmpty:" + s4.isEmpty()); // NullPointerException // s5對象都不存在,因此不能調用方法,空指針異常 // System.out.println("isEmpty:" + s5.isEmpty()); } }
String類的獲取功能3d
package cn.itcast_04; /* * String類的獲取功能 * int length():獲取字符串的長度。 * char charAt(int index):獲取指定索引位置的字符 * int indexOf(int ch):返回指定字符在此字符串中第一次出現處的索引。 * 爲何這裏是int類型,而不是char類型? * 緣由是:'a'和97其實均可以表明'a'。若是裏面寫char,就不能寫數字97了 * int indexOf(String str):返回指定字符串在此字符串中第一次出現處的索引。 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置後第一次出現處的索引。 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中從指定位置後第一次出現處的索引。 * String substring(int start):從指定位置開始截取字符串,默認到末尾。 * String substring(int start,int end):從指定位置開始到指定位置結束截取字符串。 */ public class StringDemo { public static void main(String[] args) { // 定義一個字符串對象 String s = "helloworld"; // int length():獲取字符串的長度。 System.out.println("s.length:" + s.length());//10 System.out.println("----------------------"); // char charAt(int index):獲取指定索引位置的字符 System.out.println("charAt:" + s.charAt(7));// System.out.println("----------------------"); // int indexOf(int ch):返回指定字符在此字符串中第一次出現處的索引。 System.out.println("indexOf:" + s.indexOf('l')); System.out.println("----------------------"); // int indexOf(String str):返回指定字符串在此字符串中第一次出現處的索引。 System.out.println("indexOf:" + s.indexOf("owo")); System.out.println("----------------------"); // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置後第一次出現處的索引。 System.out.println("indexOf:" + s.indexOf('l', 4)); System.out.println("indexOf:" + s.indexOf('k', 4)); // -1 System.out.println("indexOf:" + s.indexOf('l', 40)); // -1 System.out.println("----------------------"); // 本身練習:int indexOf(String str,int // fromIndex):返回指定字符串在此字符串中從指定位置後第一次出現處的索引。 // String substring(int start):從指定位置開始截取字符串,默認到末尾。包含start這個索引 System.out.println("substring:" + s.substring(5)); System.out.println("substring:" + s.substring(0)); System.out.println("----------------------"); // String substring(int start,intend):從指定位置開始到指定位置結束截取字符串。 //包括start索引可是不包end索引 System.out.println("substring:" + s.substring(3, 8)); System.out.println("substring:" + s.substring(0, s.length())); } }
字符串遍歷:指針
package cn.itcast_04; /* * 需求:遍歷獲取字符串中的每個字符 * * 分析: * A:如何可以拿到每個字符呢? * char charAt(int index) * B:我怎麼知道字符到底有多少個呢? * int length() */ public class StringTest { public static void main(String[] args) { // 定義字符串 String s = "helloworld"; for (int x = 0; x < s.length(); x++) { System.out.println(s.charAt(x)); } } }
統計大寫字母,小寫字母,數字在字符串中的個數
package cn.itcast_04; /* * 需求:統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數。(不考慮其餘字符) * 舉例: * "Hello123World" * 結果: * 大寫字符:2個 * 小寫字符:8個 * 數字字符:3個 * * 分析: * 前提:字符串要存在 * A:定義三個統計變量 * bigCount=0 * smallCount=0 * numberCount=0 * B:遍歷字符串,獲得每個字符。 * length()和charAt()結合 * C:判斷該字符究竟是屬於那種類型的 * 大:bigCount++ * 小:smallCount++ * 數字:numberCount++ * * 這道題目的難點就是如何判斷某個字符是大的,仍是小的,仍是數字的。 * ASCII碼錶: * 0 48 * A 65 * a 97 * 雖然,咱們按照數字的這種比較是能夠的,可是想多了,有比這還簡單的 * char ch = s.charAt(x); * * if(ch>='0' && ch<='9') numberCount++ * if(ch>='a' && ch<='z') smallCount++ * if(ch>='A' && ch<='Z') bigCount++ * D:輸出結果。 * * 練習:把給定字符串的方式,改進爲鍵盤錄入字符串的方式。 */ public class StringTest2 { public static void main(String[] args) { //定義一個字符串 String s = "Hello123World"; //定義三個統計變量 int bigCount = 0; int smallCount = 0; int numberCount = 0; //遍歷字符串,獲得每個字符。 for(int x=0; x<s.length(); x++){ char ch = s.charAt(x); //判斷該字符究竟是屬於那種類型的,char類型會轉成int類型 if(ch>='a' && ch<='z'){ smallCount++; }else if(ch>='A' && ch<='Z'){ bigCount++; }else if(ch>='0' && ch<='9'){ numberCount++; } } //輸出結果。 System.out.println("大寫字母"+bigCount+"個"); System.out.println("小寫字母"+smallCount+"個"); System.out.println("數字"+numberCount+"個"); } }
String的轉換功能:
package cn.itcast_05; /* * String的轉換功能: * byte[] getBytes():把字符串轉換爲字節數組。 * char[] toCharArray():把字符串轉換爲字符數組。 * static String valueOf(char[] chs):把字符數組轉成字符串。 * static String valueOf(int i):把int類型的數據轉成字符串。 * 注意:String類的valueOf方法能夠把任意類型的數據轉成字符串。 * String toLowerCase():把字符串轉成小寫。 * String toUpperCase():把字符串轉成大寫。 * String concat(String str):把字符串拼接。 */ public class StringDemo { public static void main(String[] args) { // 定義一個字符串對象 String s = "JavaSE"; // byte[] getBytes():把字符串轉換爲字節數組。 byte[] bys = s.getBytes(); for (int x = 0; x < bys.length; x++) { System.out.println(bys[x]); } System.out.println("----------------"); // char[] toCharArray():把字符串轉換爲字符數組。 char[] chs = s.toCharArray(); for (int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("----------------"); // static String valueOf(char[] chs):把字符數組轉成字符串。 String ss = String.valueOf(chs); System.out.println(ss); System.out.println("----------------"); // static String valueOf(int i):把int類型的數據轉成字符串。 int i = 100; String sss = String.valueOf(i); System.out.println(sss); System.out.println("----------------"); // String toLowerCase():把字符串轉成小寫。 System.out.println("toLowerCase:" + s.toLowerCase()); System.out.println("s:" + s); // System.out.println("----------------"); // String toUpperCase():把字符串轉成大寫。 System.out.println("toUpperCase:" + s.toUpperCase()); System.out.println("----------------"); // String concat(String str):把字符串拼接。 String s1 = "hello"; String s2 = "world"; String s3 = s1 + s2; String s4 = s1.concat(s2); System.out.println("s3:"+s3); System.out.println("s4:"+s4); } }
把一個字符串的首字母轉成大寫,其他爲小寫。(只考慮英文大小寫字母字符)
package cn.itcast_05; /* * 需求:把一個字符串的首字母轉成大寫,其他爲小寫。(只考慮英文大小寫字母字符) * 舉例: * helloWORLD * 結果: * Helloworld * * 分析: * A:先獲取第一個字符 * B:獲取除了第一個字符之外的字符 * C:把A轉成大寫 * D:把B轉成小寫 * E:C拼接D */ public class StringTest { public static void main(String[] args) { // 定義一個字符串 String s = "helloWORLD"; // 先獲取第一個字符 String s1 = s.substring(0, 1); // 獲取除了第一個字符之外的字符 String s2 = s.substring(1); // 把A轉成大寫 String s3 = s1.toUpperCase(); // 把B轉成小寫 String s4 = s2.toLowerCase(); // C拼接D String s5 = s3.concat(s4); System.out.println(s5); // 優化後的代碼 // 鏈式編程 String result = s.substring(0, 1).toUpperCase() .concat(s.substring(1).toLowerCase()); System.out.println(result); } }
String類的其餘功能:
替換功能:
去除字符串兩空格
按字典順序比較兩個字符串
package cn.itcast_06; /* * String類的其餘功能: * * 替換功能: * String replace(char old,char new) * String replace(String old,String new) * * 去除字符串兩空格 * String trim() * * 按字典順序比較兩個字符串 * int compareTo(String str) * int compareToIgnoreCase(String str) */ public class StringDemo { public static void main(String[] args) { // 替換功能 String s1 = "helloworld"; String s2 = s1.replace('l', 'k'); String s3 = s1.replace("owo", "ak47"); System.out.println("s1:" + s1); System.out.println("s2:" + s2); System.out.println("s3:" + s3); System.out.println("---------------"); // 去除字符串兩空格 String s4 = " hello world "; String s5 = s4.trim(); System.out.println("s4:" + s4 + "---"); System.out.println("s5:" + s5 + "---"); // 按字典順序比較兩個字符串 String s6 = "hello"; String s7 = "hello"; String s8 = "abc"; String s9 = "xyz"; System.out.println(s6.compareTo(s7));// 0 System.out.println(s6.compareTo(s8));// 7 System.out.println(s6.compareTo(s9));// -16 } }
把數組中的數據按照指定個格式拼接成一個字符串
package cn.itcast_07; /* * 需求:把數組中的數據按照指定個格式拼接成一個字符串 * 舉例: * int[] arr = {1,2,3}; * 輸出結果: * "[1, 2, 3]" * 分析: * A:定義一個字符串對象,只不過內容爲空 * B:先把字符串拼接一個"[" * C:遍歷int數組,獲得每個元素 * D:先判斷該元素是否爲最後一個 * 是:就直接拼接元素和"]" * 不是:就拼接元素和逗號以及空格 * E:輸出拼接後的字符串 * * 把代碼用功能實現。 */ public class StringTest2 { public static void main(String[] args) { // 前提是數組已經存在 int[] arr = { 1, 2, 3 }; // 寫一個功能,實現結果 String result = arrayToString(arr); System.out.println("最終結果是:" + result); } /* * 兩個明確: 返回值類型:String 參數列表:int[] arr */ public static String arrayToString(int[] arr) { // 定義一個字符串 String s = ""; // 先把字符串拼接一個"[" s += "["; // 遍歷int數組,獲得每個元素 for (int x = 0; x < arr.length; x++) { // 先判斷該元素是否爲最後一個 if (x == arr.length - 1) { // 就直接拼接元素和"]" s += arr[x]; s += "]"; } else { // 就拼接元素和逗號以及空格 s += arr[x]; s += ", "; } } return s; } }
字符串反轉
package cn.itcast_07; import java.util.Scanner; /* * 字符串反轉 * 舉例:鍵盤錄入」abc」 * 輸出結果:」cba」 * * 分析: * A:鍵盤錄入一個字符串 * B:定義一個新字符串 * C:倒着遍歷字符串,獲得每個字符 * a:length()和charAt()結合 * b:把字符串轉成字符數組 * D:用新字符串把每個字符拼接起來 * E:輸出新串 */ public class StringTest3 { public static void main(String[] args) { // 鍵盤錄入一個字符串 Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字符串:"); String line = sc.nextLine(); String s = myReverse(line); System.out.println("實現功能後的結果是:" + s); } /* * 兩個明確: 返回值類型:String 參數列表:String */ public static String myReverse(String s) { // 定義一個新字符串 String result = ""; // 把字符串轉成字符數組 char[] chs = s.toCharArray(); // 倒着遍歷字符串,獲得每個字符 for (int x = chs.length - 1; x >= 0; x--) { // 用新字符串把每個字符拼接起來 result += chs[x]; } return result; } }
統計大串中小串出現的次數
package cn.itcast_07; /* * 統計大串中小串出現的次數 * 舉例: * 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" * 結果: * java出現了5次 * * 分析: * 前提:是已經知道了大串和小串。 * * A:定義一個統計變量,初始化值是0 * B:先在大串中查找一次小串第一次出現的位置 * a:索引是-1,說明不存在了,就返回統計變量 * b:索引不是-1,說明存在,統計變量++ * C:把剛纔的索引+小串的長度做爲開始位置截取上一次的大串,返回一個新的字符串,並把該字符串的值從新賦值給大串 * D:回到B */ public class StringTest5 { public static void main(String[] args) { // 定義大串 String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"; // 定義小串 String minString = "java"; // 寫功能實現 int count = getCount(maxString, minString); System.out.println("Java在大串中出現了:" + count + "次"); } /* * 兩個明確: 返回值類型:int 參數列表:兩個字符串 */ public static int getCount(String maxString, String minString) { // 定義一個統計變量,初始化值是0 int count = 0; int index; //先查,賦值,判斷 while((index=maxString.indexOf(minString))!=-1){ count++; maxString = maxString.substring(index + minString.length()); } return count; } }