字符串的使用
字符串的創(chuàng)建
簡單地說,使用字符串主要分為兩步
1:定義并初始化字符串
2:使用字符串,對字符串紀念性一些處理
如下所示:
//創(chuàng)建一個字符串對象"hello world"
String s = "hello world";
在Java中,字符串被作為String類型的對象來處理。String 類是Java設(shè)計人員預(yù)先提供的一個非常有用的類,他位于java.lang 保重,默認情況下,該報被自動導(dǎo)入所有的程序中。創(chuàng)建String 對象的另外兩種方法如下列代碼所示。
//創(chuàng)建一個空字符串
String s = new String();
或者
//創(chuàng)建一個字符串對象 ”hello world“
String s = new String("hello world");
案例
注冊新用戶,要求密碼長度不能小于6位
代碼
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入用戶名");
String name = scanner.next();
System.out.println("請輸入密碼");
String password = scanner.next();
if( password.length() >=6 ){
System.out.println("密碼長度符合要求");
}
else{
System.out.println("密碼長度過短");
}
// a.length數(shù)組 length 是屬性 沒有()
int [ ] a = new int[4];
System.out.println(a.length);
}
}
/**用 length()方法來得到長度
*字符串1.length();
*返回 字符串1的長度
*/
字符串比較
語法格式:
字符串1.equals(字符串2);
案例:
注冊成功之后,實現(xiàn)登錄驗證,用戶名為”小明“ 密碼為”1234567“
代碼:
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入用戶名:");
String name = scanner.next();
System.out.println("請輸入密碼:");
String password = scanner.next();
if( name.equal("小明") && password.equals("1234567")){
System.out.println("登陸成功!");
}else {
System.out.println("登陸失敗");
}
}
}
在Java中雙等號(==)和equals()方法隨都應(yīng)用與兩個字符串,但所判斷的內(nèi)容是有差別的;簡單來說,”==“判斷的是兩個字符串對象在內(nèi)存中的首地址是否相等,及判斷是否是同一個字符串對象,而 equals()判斷的是兩個字符串對象的值是否相等
案例:系統(tǒng)規(guī)定,登錄時不考慮用戶名的大小寫問題
代碼:
使用equalsIgnoreCase()方法
lgnore是”忽略“的意思
這種方法在比較字符串是會忽略字符的大小寫
語法格式:
字符串1.equalsIgnoreCase(字符串2)
//忽略大小寫比較字符串1和字符串2,如果都相同,則返回true 否則返回false
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入用戶名:");
String name = scanner.next();
System.out.println("請輸入密碼:");
String password = scanner.next();
if( name.equalsIgnoreCase("小明") && password.equalsIgnoreCase("1234567")){
System.out.println("登陸成功!");
}else {
System.out.println("登陸失敗");
}
在Java中 String類提供了兩個方法改變字符串中字符的大小寫
//toLowerCase 轉(zhuǎn)為小寫
//toUpperCase 轉(zhuǎn)為大寫
代碼
public class Demo03 {
public static void main(String[] args) {
// toLowerCase 轉(zhuǎn)為小寫
// toUpperCase 轉(zhuǎn)為大寫
String s = "Tom";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
}
}
字符串的拼接:
語法格式:
字符串.concat(字符串2);
字符串2被鏈接到字符串1的后面
代碼:
public class Demo04 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println((s1 + s2)); //helloworld
System.out.println(s1.concat(s2)); //helloworld
System.out.println(s2.concat(s1));//worldhello
}
}
字符串的提取與查詢:
indexOf //返回出現(xiàn)第一個匹配字符 如果沒有找到匹配 則返回-1
lastIndexOf // 搜索最后一個出現(xiàn)的字符(或字符串)的位置
9:substring //(開始位置) 截取元素
10.substring //(2,4) 按[2,4)截取元素
11.trim() //去掉字符串前后的空格
代碼:
public class Demo04 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println(s1.indexOf("l"));
System.out.println(s1.lastIndexOf("l"));
System.out.println(s1.substring(2)); // llo
System.out.println(s2.substring(2,4)); // rl 包含2 不包含4
String s3 = " haha hehe heihei ";
System.out.println(s3); // haha hehe heihei
System.out.println(s3.trim()); // haha hehe heihei
}
}
字符串拆分:
語法格式
字符串1.split(String separator,int limit);
//separator可選項,標(biāo)識拆分字符串時使用一個或多個字符。如果不選擇該項,則返回包含該字符串所有單個字符串的元素數(shù)組
//limit 可選項,該值用來限制返回數(shù)組中的元素個數(shù)
代碼:
public class Demo04 {
public static void main(String[] args) {
String gc = "長亭外 古道邊 芳草碧連天";
String[] s = gc.split("");
System.out.println(Arrays.toString(s));
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}