第19天学习Java的笔记-String字符串
生活随笔
收集整理的這篇文章主要介紹了
第19天学习Java的笔记-String字符串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
29天!
字符串
1.字符串概述和特點、構造方法和直接創建
package Demo1901;/* * java.lang.String類代表字符串。 * API當中說,Java程序中的所有字符串字面值(如“abc”)都作為此類的實例實現。 * 其實就是說,程序當中所有的雙引號字符串,都是String類的對象。(就算沒有new,也照樣是) * * 字符串的特點: * 1.字符串的內容永不可變【重點】。 * 2.正是因為字符串不可改變,所以字符串是可以共享的。 * 3.字符串效果上相當于是char[]字符數組。但是底層原理是byte[]字節數組。 * * 創建字符串的常見3+1種方式。 * 三種構造方法: * public String();創建一個空白字符串,不含任何內容。 * public String(char[] array);根據字符數組的內容,來創建對應的字符串。 * public String(byte[] array);根據字節數組的內容,來創建對應的字符串。 * 一種直接創建: * String str = "hello";//右邊直接寫雙引號 * * 注意:直接寫上雙引號,就是字符串對象。 * */ public class Demo01String {public static void main(String[] args) {//首先使用空參構造String str1 = new String();System.out.println("第一個字符串為:" + str1);//根據字符數組創建字符串char[] charArray = {'A', 'B', 'C'};String str2 = new String(charArray);System.out.println("第二個字符串為:" + str2);//根據字節數組創建字符串byte[] byteArray = {97, 98, 99};String str3 = new String(byteArray);System.out.println("第三個字符串為:" + str3);//第三個字符串為:abc//直接創建String str4 = "hello";System.out.println("第四個字符串為:" + str4);} }2.字符串的常量池
package Demo1901; //雙引號直接寫的在常量池當中,new的不在池當中 /* * 字符串常量池:程序當中直接寫上的雙引號字符串,就在字符串常量池中。 * * 對于基本類型來說,==就是數值的比較。 * 對于引用類型來說,==是進行【地址值】的比較。 * */public class Demo02StringPool {public static void main(String[] args) {String str1 = "abc";String str2 = "abc";char[] charArray = {'a', 'b', 'c'};String str3 = new String(charArray);System.out.println(str1 == str2);//trueSystem.out.println(str1 == str3);//falseSystem.out.println(str2 == str3);//false} }3.字符串的比較相關方法
package Demo1901;/** ==是進行對象的地址值比較,如果確實需要字符串的內容比較,可以使用兩個方法,** public boolean equals(Object obj):參數可以是任何對象,* 只有參數是一個字符串且內容相同的才會給true,否則返回false。* 注意事項:* 1.任何對象都能用Object進行接收。* 2.equals方法具有對稱性,也就是a.equals(b)和b.equals(a)效果一樣。* 3.如果比較雙方一個常量一個變量,推薦把常量字符寫在前面。* 推薦:“abc”.equals(str)** public boolean equalsIgnoreCase(String str);忽略大小寫進行比較* */ public class Demo03StringEquals {public static void main(String[] args) {String str1 = "Hello";String str2 = "Hello";char[] charArray = {'H','e','l','l','o'};String str3 = new String(charArray);//boolean b1 = str1.equals(str2);//boolean b2 = str1.equals(str3);//boolean b3 = str3.equals("Hello");System.out.println(str1.equals(str2));//trueSystem.out.println(str1.equals(str3));//trueSystem.out.println(str3.equals("Hello"));//trueSystem.out.println("Hello".equals(str2));//trueString str5 = null;System.out.println("abc".equals(str5));//推薦:false//System.out.println(str5.equals("abc"));//不推薦,報錯,空指針異常NullPointerExceptionString str6 = "HAHa";String str7 = "haha";System.out.println(str6.equalsIgnoreCase(str7));//true} }4.字符串的獲取相關方法
package Demo1901;/* * String當中與獲取相關的常用方法有: * Public int length():獲取當前字符串中含有的字符個數,拿到字符長度。 * public String concat(String str):將當前字符串和參數字符串拼接成為返回值新的字符串 * public char charAt(int index):獲取指定索引位置的單個字符。(索引從0開始) * public int indexOf(String str):查找參數字符串在本字符串當中首次出現的索引位置,如果沒有,返回-1值。 * */public class Demo04StringGet {public static void main(String[] args) {//1.獲取字符串的長度int length = "shbhdsgvhsdbnc".length();System.out.println("字符串的長度為:" + length);//字符串的長度為:14//2.拼接字符串String str1 = "hello";String str2 = "Hello";String concat = str1.concat(str2);//全新的第三個字符串,不影響str1和str2,因為字符串是常量System.out.println(str1);//helloSystem.out.println(str2);//HelloSystem.out.println("拼接字符串為:" + concat);//拼接字符串為:helloHello//3.獲取指定索引位置的單個字符char c = str1.charAt(1);System.out.println("指定索引位置的單個字符為" + c);//指定索引位置的單個字符為e//4.查找參數字符串在本字符串當中首次出現的索引位置String str3 = "helloHelloHello";String str4 = "Hello";int i = str3.indexOf(str4);int i1 = str4.indexOf(str3);System.out.println("首次出現的位置為:" + i);//5,索引仍然從0開始System.out.println("首次出現的位置為:" + i1);//-1} }5.字符串的截取
package Demo1901;/* * 字符串的截取方法: * public String substring(int index):截取從參數位置一直到字符串末尾,返回新字符串。 * public String substring(int begin,int end):截取從begin開始,一直到end結束,中間的字符串。 * 備注:[begin,end):包含左邊,不包含右邊。類似于random * */public class Demo05SubString {public static void main(String[] args) {String str1 = "HelloWorld";String substring1 = str1.substring(5);System.out.println(str1);//HelloWorldSystem.out.println(substring1);//WorldString substring2 = str1.substring(4, 7);System.out.println(substring2);//owo//下面這種寫法,字符串的內容仍然是沒有改變的//下面有兩個字符串:“Hello”,“java”//strA當中保存的是地址值//本來地址值是hello的0x666;//后來地址值變成了Java的0x999;String strA = "hello";System.out.println(strA);//hellostrA = "java";System.out.println(strA);//java} }6.字符串的轉換相關方法
package Demo1901;/* * String當中與轉換相關的常用方法有: * public char[] toCharArray():將當前字符串拆分為字符數組作為返回值。 * public byte[] getBytes():獲得當前字符串底層的字節數組。 * public String replace(CharSequence oldString,CharSequence newString): * 將所有出現的老字符串替換為新的字符串,返回替換之后的結果新字符串。 * CharSequence的意思就是可以接收字符串 * */public class Demo06StringConvert {public static void main(String[] args) {//1.轉換為字符數組char[] chars = "Hello".toCharArray();for (int i = 0; i < chars.length; i++) {System.out.print(chars[i]);//Hello}System.out.println("======");//2.轉換為字節數組byte[] bytes = "abc".getBytes();for (int i = 0; i < bytes.length; i++) {System.out.print(bytes[i]);}//3.替換String str1 = "helloWorld";String replace1 = str1.replace("World", "java");System.out.println(str1);//helloWorldSystem.out.println(replace1);//hellojavaString replace2 = str1.replace("l", "*");System.out.println(str1);//helloWorldSystem.out.println(replace2);//he**oWor*d//4.替換在生活中的應用,游戲中的敏感詞匯替換String str2 = "會不會玩啊,你大爺的";String replace3 = str2.replace("你大爺的", "****");System.out.println(replace3);//會不會玩啊,****} }7.字符串的分割方法
package Demo1901;/* * 分割字符串的方法: * public String[] split(String regex):按照參數的規則,將字符串切分成若干部分。 * * 注意事項: * spilt方法的參數其實是一個“正則表達式”,以后學習。 * 但是要注意,如果按照英文句點“.”進行切分,必須寫“\\.”(兩個反斜杠) * */public class Demo07StringSplit {public static void main(String[] args) {String str1 = "aaa,bbb,ccc";String[] split = str1.split(",");for (int i = 0; i < split.length; i++) {System.out.println("分割結果為:" + split[i]);//分割結果為:aaa 分割結果為:bbb 分割結果為:ccc}//System.out.println("分割結果為:" + split);//分割結果為:[Ljava.lang.String;@312b1daeSystem.out.println("=====");String str2 = "aaa bbb ccc";String[] split1 = str2.split(" ");for (int i = 0; i < split1.length; i++) {System.out.println(split1[i]);}String str3 = "aaa.bbb.ccc";//String[] split2 = str3.split(".");//無法輸出結果,說明循環未執行//0String[] split2 = str3.split("\\.");System.out.println(split2.length);for (int i = 0; i < split2.length; i++) {System.out.println(split2[i]);}} }8.按指定格式拼接字符串
package Demo1901;/* * 題目: * 定義一個方法,把數組{1,2,3}按照指定格式拼接成一個字符串。 * 格式參照如下:[word1#word2#word3] * * 思路: * 1.首先準備一個int數組 * 2.方法convertString,返回值為字符串,參數為數組 * 3.格式[word1#word2#word3]。用到for循環、字符串拼接,每個數組元素之前都有一個word字樣,分隔符使用的是# * * 注意事項: * 在方法中,每次拼接的是上次的結果,而不是原字符串 * */public class Demo08StringPractice01 {public static void main(String[] args) {int[] array = {1, 2, 3};System.out.println(convertString(array));}/*public static String convertString(int[] array){String str = "";//String result = "";//將后面的str換為result會導致只輸出[word3]System.out.print("[");for (int i = 0; i < array.length; i++) {if (i == (array.length-1)){str = str.concat("word" + array[i] +"]");}else {str = str.concat("word" + array[i] + "#");}}return str ;}*///更簡單的思路,直接將字符串初始化為左括號,后面直接用+=連接public static String convertString(int[] array) {String str = "[";for (int i = 0; i < array.length; i++) {if (i == array.length-1){str += "word" + array[i] + "]";}else {str += "word" + array[i] + "#";}}return str;} }9.練習
9.1題目1:
定義一個方法,把數組{1,2,3}按照指定格式拼接成一個字符串。格式參照如下:[word1#word2#word3]
package Demo1901;/* * 題目: * 定義一個方法,把數組{1,2,3}按照指定格式拼接成一個字符串。 * 格式參照如下:[word1#word2#word3] * * 思路: * 1.首先準備一個int數組 * 2.方法convertString,返回值為字符串,參數為數組 * 3.格式[word1#word2#word3]。用到for循環、字符串拼接,每個數組元素之前都有一個word字樣,分隔符使用的是# * * 注意事項: * 在方法中,每次拼接的是上次的結果,而不是原字符串 * */public class Demo08StringPractice01 {public static void main(String[] args) {int[] array = {1, 2, 3};System.out.println(convertString(array));}/*public static String convertString(int[] array){String str = "";//String result = "";//將后面的str換為result會導致只輸出[word3]System.out.print("[");for (int i = 0; i < array.length; i++) {if (i == (array.length-1)){str = str.concat("word" + array[i] +"]");}else {str = str.concat("word" + array[i] + "#");}}return str ;}*///更簡單的思路,直接將字符串初始化為左括號,后面直接用+=連接public static String convertString(int[] array) {String str = "[";for (int i = 0; i < array.length; i++) {if (i == array.length-1){str += "word" + array[i] + "]";}else {str += "word" + array[i] + "#";}}return str;} }9.2 題目2:
鍵盤輸入一個字符串,并且統計其中各種字符出現的次數。種類有:大寫字母、小寫字母、數字、其他
package Demo1901;/** 題目:* 鍵盤輸入一個字符串,并且統計其中各種字符出現的次數。* 種類有:大寫字母、小寫字母、數字、其他** 思路:* 1.鍵盤輸入* 2.將字符串轉化為字符數組,判斷數組每個元素的所處位置,符合某項則加一* 數字:48-57(0-9);大寫字母:65-90;小寫字母:97-122* 另外一種思路,可以不查表,直接ch >= '0' && ch <= '9'* */import java.util.Scanner;public class Demo09StringPractice02Count {public static void main(String[] args) {//鍵盤輸入獲取字符串Scanner sc = new Scanner(System.in);System.out.println("請輸出字符串:");String str = sc.next();//將輸入字符串轉換為字符數組char[] chars = str.toCharArray();//定義各個類型個數int num = 0, upperCase = 0, lowerCase = 0, other = 0;//for循環遍歷字符數組for (int i = 0; i < chars.length; i++) {//第一種思路:查表,很麻煩/*if (chars[i] >= 48 && chars[i] <= 57) {num++;} else if (chars[i] >= 65 && chars[i] <= 90) {upperCase++;} else if (chars[i] >= 97 && chars[i] <= 122) {lowerCase++;} else {other++;}*///第二種思路:不查表,容易if (chars[i] >= '0' && chars[i] <= '9'){num ++;} else if (chars[i] >= 'A' && chars[i] <= 'Z'){upperCase++;} else if (chars[i] >= 'a' && chars[i] <= 'z'){lowerCase++;} else {other++;}}System.out.println("數字為" + num + "個");System.out.println("大寫字母為" + upperCase + "個");System.out.println("小寫字母為" + lowerCase + "個");System.out.println("其他字符為" + other + "個");} }總結
以上是生活随笔為你收集整理的第19天学习Java的笔记-String字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java循环案例-银行存钱问题
- 下一篇: 第20天学习Java的笔记-static