1.什么是字符串
- 使用双引号引住的任意个字符,称做字符串。
例如
public static void main(String[] args) {
String name ="hello";
String pass ="buzhidao";
}
2.字符串分类
java创建字符串有两种方式
- 字面量
1String str ="haha";
- 字符串对象
String str = new String("haha");
3.字面量与字符串对象的区别
字面量
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);
}