2019-05-21 Java学习日记之String类型Demo
String類的構造方法:
public String():空構造
public String(byte[ ]?bytes):把字節數組轉成字符串
public String(byte[ ]?bytes,int?index,int?length):把字節數組的一部分轉成字符串
public String(char[ ]?value):把字符數組轉成字符串
public String(char[ ]?value,int?index,int?count):把字符數組的一部分轉成字符串
public String(String?original):把字符串常量值轉成字符串
package com.test;public class Demo {public static void main(String[] args) {String s1 = new String();System.out.println(s1);byte[] arr1 = {97,98,99};String s2 = new String(arr1); //解碼,將計算機讀的懂的轉換成我們讀得懂的System.out.println(s2); byte[] arr2 = {97,98,99,100,101,102}; String s3 = new String(arr2, 2, 3);//將arr2字節數組從2索引開始轉換3個 System.out.println(s3); char[] arr3 = {'a','b','c','d','e'};//將字符數組轉換成字符串 String s4 = new String(arr3); System.out.println(s4); String s5 = new String(arr3, 1, 3);//將arr3字符數組,從1索引開始轉換3個 System.out.println(s5); String s6 = new String("hello"); System.out.println(s6); } }?
String類的常見面試題:
1、判斷定義為String類型的s1和s2是否相等
String?s1 = “abc”;
String?s2 = “abc”;
System.out.println(s1 ==?s2);
System.out.println(s1.equals(s2));
2、下面這句話在內存中創建了幾個對象?
String?s1 =?new String(“abc”);
3、判斷定義為String類型的s1和s2是否相等
String?s1 =?new String(“abc”);
String?s2 = “abc”;
System.out.println(s1 ==?s2);
System.out.println(s1.equals(s2));
4、判斷定義為String類型的s1和s2是否相等
String?s1 = “a” + “b” + “c”;
String?s2 = “abc”;
System.out.println(s1 ==?s2);
System.out.println(s1.equals(s2));
5、判斷定義為String類型的s1和s2是否相等
String?s1 = “ab”;
String?s2 = “abc”;
String?s3 =?s1 +“c”;
System.out.println(s3 ==?s2);
System.out.println(s3.equals(s2));
private static void demo1() {String s1 = "abc";String s2 = "abc";System.out.println(s1 == s2); //true System.out.println(s1.equals(s2)); //true}private static void demo2() {//創建幾個對象//創建兩個對象,一個在常量池中,一個在堆內存中String s1 = new String("abc"); System.out.println(s1); } private static void demo3() { String s1 = new String("abc"); String s2 = "abc"; System.out.println(s1 == s2); //false System.out.println(s1.equals(s2)); //true } private static void demo4() { //byte b = 3 + 4; //在編譯時就變成7,把7賦值給b,常量優化機制 String s1 = "a" + "b" + "c"; String s2 = "abc"; System.out.println(s1 == s2); //true System.out.println(s1.equals(s2)); //true } private static void demo5() { String s1 = "ab"; String s2 = "abc"; String s3 = s1 + "c"; System.out.println(s3 == s2); //false System.out.println(s3.equals(s2)); //true }?
String類的判斷功能:
boolean?equals(Object?obj):比較字符串的內容是否相等,區分大小寫
boolean?equalsIgnoreCase(String?str):比較字符串的內容是否相同,忽略大小寫
boolean?contains(String?str):判斷大字符串中是否包含小字符串
boolean?startWith(String?str):判斷字符串是否以某個指定的字符串開頭
boolean?endsWith(String?str):判斷字符串是否以某個指定的字符串結尾
boolean?isEmpty():判斷祖符串是否為空
private static void demo1() {String s1 = "hello";String s2 = "hello";String s3 = "Hello";System.out.println(s1.equals(s2)); //trueSystem.out.println(s2.equals(s3)); //falseSystem.out.println(s1.equalsIgnoreCase(s2)); //trueSystem.out.println(s1.equalsIgnoreCase(s3)); //true 不區分大小寫} private static void demo2() { String s1 = "我愛學習,哈哈"; String s2 = "學習"; String s3 = "xuexi"; String s4 = "我愛"; String s5 = "哈哈"; System.out.println(s1.contains(s2)); //true 判斷是否包含傳入的字符串 System.out.println(s1.contains(s3)); //false System.out.println(s1.startsWith(s4)); //true 判斷是否以傳入的字符串開頭 System.out.println(s1.startsWith(s5)); //false System.out.println(s1.endsWith(s4)); //false 判斷是否與傳入的字符串結尾 System.out.println(s1.endsWith(s5)); //true } private static void demo3() { String s1 = "hello"; String s2 = ""; String s3 = null; System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); System.out.println(s3.isEmpty()); //java.lang.NullPointerException}
""和null的區別:
""是字符串常量,同時也是一個String類的對象,既然是對象當然可以調用String類中的方法
null是空常量,不能調用任何的方法否則會出現空指針異常,null常量可以給任意的引用數據類型賦值
模擬用戶登錄:
需求:模擬登錄,給三次機會,并提示還有幾次。
用戶名和密碼都是admin
Demo:
package com.test;import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in); // 創建鍵盤錄入對象for (int i = 0; i < 3; i++) {System.out.println("請輸入用戶名:");String username = sc.nextLine(); // 將鍵盤錄入的用戶名存儲在username中System.out.println("請輸入密碼:");String password = sc.nextLine(); // 將鍵盤錄入的密碼存儲在password中
// 如果是字符串常量和字符串變量比較,通常都是字符串常量調用方法,將變量當作參數傳遞,防止空指針異常if ("admin".equals(username) && "admin".equals(password)) {System.out.println("歡迎" + username + "登錄");break;} else {if (i == 2) {System.out.println("請明天再來!");} else {System.out.println("用戶名和密碼錯誤!你還有" + (2 - i) + "次機會!");}}}}}
?
String類的獲取功能:
int?length():獲取字符串長度
char?charAt(int?index):獲取指定索引位置的字符
int?indexOf(int?ch):返回指定字符在此字符串中第一次出現處索引
int?indexOf(String?str):返回指定字符串在此字符串中第一次出現處的索引
int?indexOf(int?ch,int?fromIndex):返回指定字符串在此字符中從指定位置后第一次出現處的索引
int?indexOf(String?str,int?fromIndex):返回指定字符串在此字符中從指定位置后第一次出現處的索引
lastIndexOf
String?substring(int?start):從指定位置開始截取字符串,默認到末尾
String?substring(int?start,int end):從指定位置開始到指定位置結束截取字符串
private static void demo5() {String s1 = "woaixuexi";s1.substring(4); //subString會產生一個新的字符串,需要將新的字符串記錄 System.out.println(s1);}private static void demo4() {String s1 = "hellokugou";String s2 = s1.substring(5);System.out.println(s2);String s3 = s1.substring(0,5);//包含頭,不包含尾,左閉右開 System.out.println(s3);}private static void demo3() {String s1 = "woaixuexi";int index = s1.indexOf('i',4); //從指定位置開始向后找 System.out.println(index);int index2 = s1.lastIndexOf("i");//從后向前找,第一次出現的字符 System.out.println(index2);int index3 = s1.lastIndexOf('i',7);//從指定位置向前找 System.out.println(index3);}private static void demo2() {String s1 = "hello";int index = s1.indexOf('e'); //參數接收的是int類型的,傳遞char類型的會自動提升 System.out.println(index);int index2 = s1.indexOf('a'); //如果不存在則返回-1 System.out.println(index2);int index3 = s1.indexOf("el"); //獲取字符串中第一個字符出現的位置 System.out.println(index3);int index4 = s1.indexOf("eo");System.out.println(index4);}private static void demo1() {//int[] arr = {11,22,33};//System.out.println(arr.length); //數組中的length是屬性 String s1 = "hello";System.out.println(s1.length()); //length()是一個方法 String s2 = "我愛學習,造嗎?";System.out.println(s2.length()); char c = s2.charAt(5); //根據索引獲取對應位置的字符 System.out.println(c);char c2 = s2.charAt(10);//StringIndexOutOfBoundsException字符串索引越界異常 System.out.println(c2);}?
遍歷數組:
private static void demo() {String s = "hello";for (int i = 0; i < s.length(); i++) { //通過for循環獲取到字符串中的每個字符的索引System.out.println(s.charAt(i)); //通過索引獲取每一個字符 }}?
統計不同類型字符個數:
public class Test2 {/*** 需求:統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數,其他字符出現的次數。 ABCDEabcd123456!@#$%^* 分析:字符串是由字符組成的,而字符的值都是有范圍的,通過范圍來判斷是否包含該字符 如果包含就讓計數器自增*/public static void main(String[] args) {String s = "ABCDEabcd123456!@#$%^";int big = 0;int small = 0;int num = 0;int other = 0;// 1、獲取每一個字符,通過for循環遍歷for (int i = 0; i < s.length(); i++) {char c = s.charAt(i); // 通過索引獲取每一個字符// 2、判斷字符是否在這個范圍內if (c >= 'A' && c <= 'Z') {big++; // 如果滿足是大寫字母,就讓其對應的變量自增} else if (c >= 'a' && c <= 'z') {small++;} else if (c >= '0' && c <= '9') {num++;} else {other++;}}// 3、打印每一個計數器的結果System.out.println(s + "中大寫字母有:" + big + "個,小寫字母有:" + small + "個,數字字符有:" + num + "個,其他字符有:" + other + "個");}?
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):把字符串拼接
private static void demo3() {char[] arr = {'a','b','c'};String s = String.valueOf(arr);//底層是由string類的構造方法完成的 System.out.println(s);String s2 = String.valueOf(100);//將100轉換成字符串System.out.println(s2 + 100);}private static void demo2() {String s1 = "hello";char[] arr = s1.toCharArray(); //將字符串轉換為字符數組for (int i = 0; i < arr.length; i++) {System.out.print(arr[i] + " ");}}private static void demo1() {String s1 = "abc";byte[] arr1 = s1.getBytes();for (int i = 0; i < arr1.length; i++) {//System.out.print(arr1[i] + " "); }String s2 = "我愛學習";byte[] arr2 = s2.getBytes(); //通過gdk碼表將字符串轉換成字節數組for (int i = 0; i < arr2.length; i++) { //編碼:把我們看得懂的轉換成計算機讀得懂的//System.out.print(arr2[i] + " "); //gdk碼表一個中文代表兩個字節} //gdk碼表特點,中文的第一個字節肯定是負數 String s3 = "琲";byte[] arr3 = s3.getBytes();for (int i = 0; i < arr3.length; i++) {System.out.print(arr3[i] + " ");}}?
?鏈式編程:
public class Test3 {/*** 需求:把一個字符串的首字母轉換成大寫,其余為小寫* 鏈式編程:只要保證每次調用完方法返回的是對象,就可以繼續調用*/public static void main(String[] args) {String s1 = "woaiXuEXiya";String s2 = s1.substring(0, 1).toUpperCase().concat(s1.substring(1).toLowerCase());System.out.println(s2);}?把數組轉成字符串:
public class Test4 {/*** 需求:把數組中的數據按照指定個格式拼接成一個字符串* 舉例:* int[] arr = {1,2,3}; * 輸出結果:* "[1, 2, 3]"* 分析:* 1、需要定義一個字符串”[“* 2、遍歷數組獲取每一個元素* 3、用字符串與數組中的元素進行拼接*/public static void main(String[] args) {int[] arr = {1,2,3};String s = "[";for (int i = 0; i < arr.length; i++) {if (i == arr.length - 1) {s = s +arr[i] + "]";}else {s = s +arr[i] + ", ";}}System.out.println(s);}?
?字符串反轉:
public class Test5 {public static void main(String[] args) {Scanner sc = new Scanner(System.in); //創建鍵盤錄入對象System.out.println("");String line = sc.nextLine(); //將鍵盤錄入的字符串存儲在line中char[] arr = line.toCharArray(); //將字符串轉換為字符數組 String s = "";for (int i = 0; i < arr.length; i++) { //倒看遍歷字符數組s = s + arr[i]; //拼接成字符串 }System.out.println(s);}?
轉載于:https://www.cnblogs.com/clqbolg/p/10903499.html
總結
以上是生活随笔為你收集整理的2019-05-21 Java学习日记之String类型Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分析工厂模式中的问题并改造
- 下一篇: 前端进阶之说一说你对HTML5语义化的理