1.什么是字符串
- 使用雙引號引住的任意個字符,稱做字符串。
例如
public static void main(String[] args) {
String name ="hello";
String pass ="buzhidao";
}
2.字符串分類
java創(chuàng)建字符串有兩種方式
- 字面量
1String str ="haha";
- 字符串對象
String str = new String("haha");
3.字面量與字符串對象的區(qū)別
字面量
String str1 = "yaya";
String str2 = "yaya";
System.out.println(str1 == str2);
}
字符串對象
public static void main(String[] args) {
String str1 = new String("yaya");
String str2 = new String("yaya");
System.out.println(str1 == str2);
}
4.String類常用的API
字符串連接 concat
public static void main(String[] args) {
String firstName = "李";
String lastName = "逵";
String fullName = firstName.concat(lastName);
System.out.println(fullName);//李逵
}
字符串相等比較 equals
public static void main(String[] args) {
String firstName = "李";
String lastName = "李";
//判斷firstName和lastName指向同一塊內存嗎塌计,比較的是值幻工,不是內容
boolean result1 = firstName.equals(lastName);
boolean result2 = firstName == lastName;
System.out.println(result1);//true
System.out.println(result2);//true
}
字符串忽略大小寫相等比較 equalsIgnoreCase
public static void main(String[] args) {
String firstName = "li";
String lastName = "Li";
//判斷firstName和lastName指向同一塊內存嗎雨效,比較的是值微宝,不是內容
boolean result1 = firstName.equalsIgnoreCase(lastName);
System.out.println(result1);//true
}
字符串長度length()
數組的大小是屬性:arr.length
字符串的大小是方法:str.length()
public static void main(String[] args) {
String username = "admin";
int length = username.length();
System.out.println(length); // 5
}
字符串全轉換大寫或者小寫toUpperCase珠叔,toLowerCase
public static void main(String[] args) {
String word = "aoHaYgaoZayiMskongbaWAATAXIWA";
System.out.println(word.toUpperCase());
System.out.println(word);
}
運行結果:AOHAYGAOZAYIMSKONGBAWAATAXIWA
public static void main(String[] args) {
String word = "aoHaYgaoZayiMskongbaWAATAXIWA";
System.out.println(word.toLowerCase());
System.out.println(word);
}
運行結果:aohaygaozayimskongbawaataxiwa
切割字符串
判斷”admin@itlaobing.com“ 這個郵箱中是否只包含一個@符合
方法1:
public static void main(String[] args) {
String mail = "admin@itlaobing@.com";
int index1 = mail.indexOf("@");
int index2 = mail.lastIndexOf("@");
System.out.println(index1);//5
System.out.println(index2);//15
if((index1 == index2)&& index1!=-1 && index2!=-1){
System.out.println("合法");
}else{
System.out.println("不合法");
}
}
方法2:
public static void main(String[] args) {
String mail = "admin@itlaobing.com";
String[] split = mail.split("@");
if(split.length==2){
System.out.println("合法");
}else{
System.out.println("不合法");
}
}
判斷是否以什么開頭蝎宇,什么結尾(startsWith ,endsWith)
public static void main(String[] args) {
String website = "www.itlaobing.cn";
System.out.println(website.startsWith("www"));
}
public static void main(String[] args) {
String website = "www.itlaobing.cn";
System.out.println(website.endsWith("com"));
}
替換字符串內容 replace
public static void main(String[] args) {
String str = "今天我吃了一個壞蛋黃派";
str = str.replace("壞蛋","**");
System.out.println(str);
}
結果:今天我吃了一個**黃派
截取子串 substring
public static void main(String[] args) {
String file = "d:\\2002-07-09\\javaapi\\jdk_api_1.6_zh_cn.chm";
int startIndex = file.lastIndexOf("\\")+1;
int dotIndex = file.lastIndexOf(".");
String filename = file.substring(startIndex, dotIndex);
System.out.println(filename);
}
截取字符 charAt
public static void main(String[] args) {
String str = "admin@123456";
char ch = str.charAt(5);
int index = str.indexOf("@");
System.out.println(ch);
System.out.println(index);
}