字符串
字符串:使用双引号引住的任意个字符,称为字符串。在java中使用String类表示字符串。
字符
字符:单引号引住的一个字符,用char表示,字符串是字符的数组。
字符数组转换为字符串
public static void main(String[] args) {
//定义字符数组
char ch[] = {'j','j','x','y'};
//将字符数组转换为字符串
String str = new String(ch);
System.out.println(str);
}
字符串转换为字符数组
public static void main(String[] args) {
String str = "jjxy";
char[] chars = str.toCharArray();
for (char ch:chars ) {
System.out.println(ch);
}
}
字符串与字面量
java创建字符串有两种方式
1.字面量:直接使用双引号定义的字符串叫做字面量。
2.字符串对象:使用new 运算符创建的字符串叫做字符串对象。
区别:字面量存储位置为常量池或者元空间中。字符串对象存储在堆中。
String StringBuffer StringBuilder
String
1.String声明为final的,不可被继承
2.String实现了Serializable接口:表示字符串是支持序列化的。
实现了Comparable接口:表示String可以比较大小
3.String内部定义了final char[] value用于存储字符串数据
4.String:代表不可变的字符序列。简称:不可变性。
体现:
- 当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。
- 当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
- 当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
5.通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
6.字符串常量池中是不会存储相同内容的字符串的。
StringBuff
1.java.lang.StringBuffer代表可变的字符序列,JDK1.0中声明,可以对字符串内容进行增删,此时不会产生新的对象。
2.线程安全的,效率低
3.作为参数传递时,方法内部可以改变值。
4.StringBuffer类不同于String,其对象必须使用构造器生成
- StringBuffer():初始容量为16的字符串缓冲区
- StringBuffer(int size):构造指定容量的字符串缓冲区
- StringBuffer(String str):将内容初始化为指定字符串内容
5.扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。(默认情况下,扩容为原来容量的2倍 + 2,同时将原有数组中的元素复制到新的数组中。)
StringBuilder
StringBuilder 和 StringBuffer 非常类似,均代表可变的字符序列,而且提供相关功能的方法也一样,但它是线程不安全的,所以效率高
字符串常用方法
字符串连接 +
public static void main(String[] args) {
String firstName = "李";
String lastName = "逵";
String fullName = firstName + lastName;
System.out.println(fullName);//李逵
}
字符串连接 concat
public static void main(String[] args) {
String firstName = "李";
String lastName = "逵";
String fullName = firstName.concat(lastName);
System.out.println(fullName);//李逵
}
字符串相等比较 ==
public static void main(String[] args) {
String firstName = "李";
String lastName = "李";
//判断firstName和lastName指向同一块内存吗,比较的是值,不是内容
boolean result = firstName == lastName;
System.out.println(result);//true
}
字符串相等比较 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
}
public static void main(String[] args) {
String firstName = new String("李");
String lastName = new String("李");
//判断firstName和lastName指向同一块内存吗,比较的是值,不是内容
boolean result1 = firstName.equals(lastName);
boolean result2 = firstName==lastName;
System.out.println(result1);//true
System.out.println(result2);//false
}
字符串忽略大小写相等比较 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()
public static void main(String[] args) {
String username = "admin";
int length = username.length();
System.out.println(length); // 5
}
在字符串种查找 indexOf
public static void main(String[] args) {
String filename = "D:\\2002-07-09\\JavaAPI\\JDK_API_1.8_zh_CN.CHM";
int index = filename.indexOf("JavaAPI");
System.out.println(index);//14 找不到返回-1
}
转大写 toUpperCase
public static void main(String[] args) {
String word = "aoHaYgaoZayiMskongbaWAATAXIWA";
System.out.println(word.toUpperCase());
System.out.println(word);
}
转小写 toLowerCase
public static void main(String[] args) {
String word = "aoHaYgaoZayiMskongbaWAATAXIWA";
System.out.println(word.toLowerCase());
System.out.println(word);
}
切割字符串 split
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("不合法");
}
}
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
public static void main(String[] args) {
String website = "www.itlaobing.cn";
System.out.println(website.startsWith("www"));
}
字符串结尾 endsWith
public static void main(String[] args) {
String website = "www.itlaobing.cn";
System.out.println(website.endsWith("com"));
}
去掉两边的空格 trim
public static void main(String[] args) {
String username = " admin ";
System.out.println(username.trim());//去掉两边的空格
}
替换字符串内容 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);
}
字符串格式化输出 format
public static void main(String[] args) {
String str = null;
str = String.format("见过,%s及%s", "晁天王", "众位头领");
System.out.println(str);
str = String.format("字母a的大写是:%c", 'A');
System.out.println(str);
str = String.format("3>7的结果是:%b", 3 > 7);
System.out.println(str);
str = String.format("100的一半是:%d", 100 / 2);
System.out.println(str);
// 使用printf()方法代替format方法来格式化字符串
System.out.printf("50元的书打8.5折扣是:%f 元", 50 * 0.85);
}