字符串就是用字符拼接成的文本值,字符串在存儲上相似數組,在java語言中把字符串當作對象進行處理
建立字符串
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 6 /* 7 * 引用字符串常量 8 * */ 9 String a = "時間就是金錢,個人朋友。"; 10 String b = "鋤禾日當午"; 11 String str1, str2; 12 str1 = "We are students"; 13 str2 = "We are students"; 14 15 /* 16 * 利用構造方法實例化 17 * */ 18 String c = new String("我愛清湯小肥羊"); 19 String d = new String(c); 20 21 /* 22 * 利用字符串數組實例化 23 * */ 24 char[] charArray = {'t', 'i', 'm', 'e'}; 25 String e = new String(charArray); 26 27 28 /* 29 * 提取字符數組中的一部分建立字符串對象 30 * */ 31 char[] charArray2 = {'時', '間', '就', '是', '金', '錢'}; 32 String f = new String(charArray2, 3, 2); 33 System.out.println("f: " + f); 34 35 } 36 }
字符串對象方法java
鏈接字符串數組
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 鏈接字符串 7 * */ 8 String a = "我叫李狗蛋"; 9 String b = "今年十九歲"; 10 String c = a + ',' + b; 11 String d = "我來作個自我介紹:"; 12 String f = d.concat(c); 13 14 d += c; 15 16 System.out.println("d = " + d); 17 System.out.println("f = " + f); 18 } 19 }
獲取字符串長度spa
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 獲取字符串長度 7 * */ 8 String num = "12345 6789"; 9 System.out.println("num 的長度: " + num.length()); 10 } 11 }
獲取字符串指定位置的字符rest
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 獲取字符串指定位置的字符 7 * */ 8 String str = "牀前明月光, 疑是地上霜"; 9 char chr = str.charAt(4); 10 System.out.println("字符串中索引位置爲4的字符是:" + chr); // 字符串中索引位置爲4的字符是:光 11 } 12 }
查找子字符串索引位置code
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 查找子字符或字符串索引位置 7 * 若是存在則返回索引位置,不然返回-1 8 * */ 9 String str = "We are the world"; 10 int index1 = str.indexOf('e'); 11 int index2 = str.indexOf("e33"); 12 13 System.out.println("index1: " + index1); // size: 1 14 System.out.println("index2: " + index2); // size: -1 15 } 16 }
判斷字符串首尾是否存在指定字符串對象
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 判斷字符串首尾是否存在指定字符串 7 * 若是存在返回true,不然返回false 8 * */ 9 String str = "We are the world"; 10 11 if (str.startsWith("We")) { 12 System.out.println("We 在開頭"); 13 } 14 15 if (str.endsWith("world")) { 16 System.out.println("world 在末尾"); 17 } 18 19 } 20 }
將字符串轉換成字符數組blog
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 將字符串轉換成字符數組 7 * 8 * */ 9 String str = "We are the world"; 10 11 char[] charArr = str.toCharArray(); 12 for (int i = 0; i < charArr.length; i ++) { 13 System.out.println("數組第" + i + "個元素爲:" + charArr[i]); 14 } 15 16 } 17 }
判斷子字符串是否存在索引
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 判斷子字符串是否存在 7 * */ 8 9 String str = "今天的菜譜有:蒸羊羔,蒸熊掌,蒸鹿尾,燒花鴨,燒雛雞,燒子鵝" + 10 "滷煮鹹鴨,醬雞,臘肉,松花小肚"; 11 12 System.out.println(str); // 今天的菜譜有:蒸羊羔,蒸熊掌,蒸鹿尾,燒花鴨,燒雛雞,燒子鵝滷煮鹹鴨,醬雞,臘肉,松花小肚 13 14 boolean request1 = str.contains("臘肉"); 15 System.out.println("今天有臘肉嗎?" + request1); // 今天有臘肉嗎?true 16 17 boolean request2 = str.contains("漢堡"); 18 System.out.println("今天有漢堡嗎?" + request2); // 今天有漢堡嗎?false 19 } 20 }
截取字符串字符串
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 截取字符串 7 * */ 8 9 // 截取身份號中的出生日期 10 String idNum = "123456198002157890"; 11 String year = idNum.substring(6, 10); 12 String month = idNum.substring(10, 12); 13 String day = idNum.substring(12, 14); 14 15 System.out.println("該身份證顯示的出生日期爲:"); // 該身份證顯示的出生日期爲: 16 System.out.println(year + "年" + month + "月" + day + "日"); // 1980年02月15日 17 18 } 19 }
字符串替換string
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 字符串替換 7 * */ 8 9 // 替換字符串中的錯別字 10 String str = "登陸功能介紹:用戶輸入用戶名和密碼以後,單擊'登錄'按鈕便可完成登錄操做。"; 11 String restr = str.replace("陸", "錄"); // 將字符串中全部的 '陸' 改成 '錄' 12 System.out.println("【更改前】" + str); // 輸出原字符串 13 System.out.println("【更改後】" + restr); // 輸出更改後的字符串 14 15 } 16 }
字符串分割
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 字符串分割 7 * */ 8 9 // 將菜譜中的菜品報錯在一個數組中 10 String str = "今天的菜譜有:蒸羊羔,蒸熊掌,蒸鹿尾,燒花鴨,燒雛雞,燒子鵝" + 11 "滷煮鹹鴨,醬雞,臘肉,松花小肚"; 12 String[] denal = str.split(","); 13 14 for (int i = 0; i < denal.length; i++) { 15 System.out.println("索引" + i + "的元素:" + denal[i]); 16 } 17 18 } 19 }
大小寫轉換
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 大小寫轉換 7 * */ 8 9 // 輸出字符串的大小寫格式 10 String str = "abc DEF"; 11 12 System.out.println(str.toLowerCase()); // 按照小寫格式輸出 abc def 13 System.out.println(str.toUpperCase()); // 按照大寫格式輸出 ABC DEF 14 15 } 16 }
去除空白內容
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 去除空白內容 7 * */ 8 9 // 去掉字符串兩邊的空白內容 10 String str = " abc "; 11 12 String shortStr = str.trim(); 13 System.out.println("str的原值是: [" + str + "]"); 14 System.out.println("去掉首尾空白的值:" + shortStr); 15 16 } 17 }
比較字符串的內容是否相同
1 package com.mingri.chapter_02; 2 3 public class demo1 { 4 public static void main (String[] args) { 5 /* 6 * 比較字符串是否相等 7 * */ 8 9 // 比較字符串的內容是否相同 10 String str1 = "Hello"; 11 String str2 = new String("Hello"); 12 String str3 = new String("你好"); 13 String str4 = str2; 14 15 16 System.out.println("str1 == str2 的結果:" + (str1 == str2)); // false 17 System.out.println("str1 == str3 的結果:" + (str1 == str3)); // false 18 System.out.println("str1 == str4 的結果:" + (str1 == str4)); // false 19 System.out.println("str2 == str4 的結果:" + (str2 == str4)); // true 20 21 // equals 內容相等的時候爲true 22 System.out.println("str1.equals(str2) 的結果:" + str1.equals(str2)); // true 23 System.out.println("str1.equals(str3) 的結果:" + str1.equals(str3)); // false 24 System.out.println("str1.equals(str4) 的結果:" + str1.equals(str4)); // true 25 26 } 27 }