為什么要學習String的處理呢?
開發中,對字符串的處理是非常常見的。
String是什么?可以做什么?
java.lang.String 代表字符串。可以用來創建對象封裝字符串數據,并對其進行處理。
1、創建對象
2、封裝字符串數據
3、調用String的方法
String創建對象封裝字符串數據的方法:
方式一:
Java程序中的所有字符串文字(例如:“abc”)都為此類對象。
String name = "小黑";
String SchoolName = "黑馬程序員";
方式二:
調用String類的構造器初始化字符串對象。
new String()創建字符串對象,并調用構造器初始化字符串。
package cn.ensource.string;public class StringDemo {public static void main(String[] args) {String name = "itheima";System.out.println(name);String rs1 = new String();System.out.println(rs1);String rs2 = new String("itheima");System.out.println(rs2);char[] chars = {'a', 'b', 'c'};String rs3 = new String(chars);System.out.println(rs3);byte[] bytes = {100, 101, 102};String rs4 = new String(bytes);System.out.println(rs4);}
}
運行結果:
通過構造函數創建
通過new創建的字符串對象,每一次new都會申請一個空間,雖然內容相同,但是地址值不同。
直接賦值方式創建:
以“”雙引號給出的字符串,只要字符串序列相同順序和大小相同,無論程序代碼中出現幾次,JVM都只會建立一個String對象,并在字符串池中維護。
String類的常用方法:
String提供的操作字符串數據的常用方法:
為什么是快速熟悉這些方法呢?
API是解決需求的,快速地認識他們,實實在在地解決業務需求。
package cn.ensource.string;public class StringDemo2 {public static void main(String[] args) {// 目標:快速熟悉String提供的處理字符串的方法String s = "黑馬Java";// 獲取字符串的長度System.out.println(s.length());// 提取字符串中某個索引位置處的字符char c = s.charAt(1);System.out.println(c);// 字符串的遍歷for (int i = 0; i < s.length(); i++) {char ch = s.charAt(i);System.out.print(ch);}System.out.println("--------");// 把字符串轉成字符數組,然后再進行遍歷char[] chars = s.toCharArray();for (int i = 0; i < chars.length; i++) {System.out.print(chars[i]);}System.out.println("--------");// 判斷字符串內容,內容一樣,就返回trueString s1 = new String("黑馬");String s2 = new String("黑馬");boolean rs = s.equals(s2);System.out.println(rs);System.out.println("--------");// 忽略大小寫比較字符串String c1 = "34Aefg";String c2 = "24aefg";System.out.println(c1.equals(c2));System.out.println(c1.equalsIgnoreCase(c2));System.out.println("--------");// 截取字符串內容String s3 = "Java是最好的編程語言之一";System.out.println(s3.substring(0, 8));System.out.println("--------");// 截取字符串內容,從當前位置到字符串末尾System.out.println(s3.substring(8));System.out.println("--------");// 把字符串的某個內容,替換成新內容String s3c = s3.replace("Java", "C++");System.out.println(s3c);System.out.println("--------");// 判斷字符串中是否包含某個關鍵字String info = "Java是最好的編程語言之一";boolean rs5 = info.contains("Java");System.out.println(rs5);// startwithSystem.out.println("--------");String info2 = "Java是最好的編程語言之一";boolean rs6 = info2.startsWith("Java");System.out.println(rs6);System.out.println("--------");// 分割字符串String str5 = "張無忌,周芷若,殷素素,趙敏";String[] names = str5.split(",");for(int i = 0; i < names.length; i++) {System.out.println(names[i]);}}
}
split這個成員方法,之前在python中也遇到。
如果方法不再記得了,都是可以到API文檔中查詢的。
另外:
==:
比較基本數據類型:比較具體的值。
比較引用數據類型:比較的是對象地址值。
package com.company;public class Main {public static void main(String[] args) {char[] chs = {'a', 'b', 'c'};String s1 = new String(chs);String s2 = new String(chs);String s3 = "abc";String s4 = "abc";System.out.println(s1 == s2);System.out.println(s1 == s4);System.out.println(s3 == s4);System.out.println("-------------");System.out.println(s1.equals(s2));System.out.println(s1.equals(s3));System.out.println(s3.equals(s4));}
}
運行結果:
false
false
true
-------------
true
true
true
用戶登錄案例:
import java.util.Scanner;public class Main {public static void main(String[] args) {String username = "changchunhua";String password = "chang@123";for (int i=0; i<3; i++) {Scanner sc = new Scanner(System.in);System.out.println("Please input username: ");String name = sc.nextLine();System.out.println("Please input password: ");String pwd = sc.nextLine();if (name.equals(username) && pwd.equals(password)) {System.out.println("Sign in susccessfully!");break;} else {if (2 - i == 0) {System.out.println("Your account is locked!");} else {System.out.println("Your has 2 - i times to sign in.");}}}}
}
運行結果:?
Please input username:
chang
Please input password:
chang@123
Your has 2 - i times to sign in.
Please input username:
changchun
Please input password:
chang@123
Your has 2 - i times to sign in.
Please input username:
changchunhua
Please input password:
chang@123
Sign in susccessfully!
字符串反轉:
package com.company;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("Please input a string: ");String line = sc.nextLine();String s = reverse(line);System.out.println("s: " + s);}public static String reverse(String s) {String ss = "";for(int i=s.length()-1; i>=0; i--) {ss += s.charAt(i);}return ss;}
}
運行結果:
Please input a string:
changchunhua
s: auhnuhcgnahc