1.字符串构造
注:在字符串String中 ,==
比较的是字符串的地址 , 要想比较内容是否相等 , 就用 变量1名称.equals(变量2名称) , 返回值都为true或false
除String比较特殊之外 , 其他类型的数比较大小就可以用==
String s1="hh";
String s2="hh";
System.out.println(s1==s2); //比较地址
System.out.println(s1.equals(s2)); //比较内容
( 1 ) 字符串构造有三种方法
// 1.使用常量串构造(字符串常量池)
String s1="hello"; //直接在常量池中取出,不会创建新变量
// 2.newString对象
String s2=new String("hello"); //自己创建的变量
// 3.使用字符数组构造
char[]arr={'h','e','l','l','o'};
String s3=new String(arr); //自己创建的变量
利用第一种方法是在常量池取出字符串 , 如果两个字符串相等, 则这两个变量地址相同
而二和三则是开辟了新的内存空间 , 所以地址不同
( 2 ) String是引用类型 , 内部并不存储字符串本身 , 也就是指向这个字符串地址
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1; //把s1指向字符串的地址赋予s3,即s3和s1指向的是同一个字符串
System.out.println(s1.length()); // 获取字符串长度---输出5
System.out.println(s1.isEmpty());//如果字符串长度为0,返回false
2.String对象的比较
Java中有4种比较方式
( 1 ) ==
比较是否引用同一个对象
注 : 对于内置类型 , ==
比较的是变量中的值 ; 对于引用类型==
比较的是引用中的地址
( 内置类型有byte,short,int等
引用类型包括类 , 接口 , 数组等 )
public class test2 {
public static void main(String[] args) {
int a=10;int b=10;int c=20;
System.out.println(a==b); //true
System.out.println(b==c); //false
String s1=new String("hello");
String s2=new String("hello");
String s3=new String("world");
System.out.println(s1==s2); //false
System.out.println(s2==s3); //false
}
}
( 2 ) boolean equals(Object anObject)
方法 : 按字典序比较
字典序:字符大小的顺序
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照如下规则进行比较 , 如:s1.equals(s2)
public class test2 {
public static void main(String[] args) {
String s1=new String("hello");
String s2=new String("hello");
String s3=new String("world");
System.out.println(s1.equals(s2)); //true
System.out.println(s2.equals(s3)); //false
}
}
( 3 ) int compareTo(String s)
方法 : 按照字典序比较
与equals不同的是, equals返回值类型是boolean , compareTo返回的是int类型.
compareTo的比较规则:
< 1 > 先从首字符开始,按照字典次序大小比较 , 如果出现两个字符不同 , 则返回两者的差值 ( ASCII码值 )
< 2 > 如果前n个字符相等 ( n为两字符串长度中最小值 ) , 返回值是两个字符串长度的差值
public class Test1 {
public static void main(String[] args) {
String s1=new String("abc");
String s2=new String("abc");
String s3=new String("abcd");
String s4=new String("abe");
System.out.println(s1.compareTo(s2)); //相等,返回0
System.out.println(s1.compareTo(s3)); //前三字符相等,3-4=-1
System.out.println(s1.compareTo(s4)); //e的ASCII码值比c大2
} //执行结果:0 -1 -2
}
( 4 ) int compareTolgnoreCase(String str)
方法 : 与compareTo方式相同 , 但是忽略大小写比较
public class Test1 {
public static void main(String[] args) {
String s1=new String("Abc");
String s2=new String("abc");
System.out.println(s1.compareToIgnoreCase(s2));
} //执行结果:0
}
3.字符串查找
<1>中是要下标,返回字符 , 余下都是要字符,返回下标
注
:这里的下标与数组类似 , 都是从0开始
< 1 > charAt
语法形式:char charAt(int index)
括号中输入字符下标 ( 即第几个字符 ) , 返回下标所对应的字符
如果index为负数或者越界 , 抛出IndexOutOfBoundsException异常
< 2 > int indexOf ( char )
返回字符char第一次出现的位置 , 没找到返回-1
< 3 > int indexOf( char , int formIndex)
从formIndex下标开始查找char字符 , 没找到返回-1
< 4 > int indexOf(String)
从前往后找 , 返回String字符串第一次出现的下标 , 没找到返回-1
< 5 > int indexOf(String , int formIndex)
从formIndex下标开始查找String字符串 , 没找到返回-1
< 6 > int lastIndexOf ( char )
从后往前找 , 返回字符char第一次出现位置的下标 , 没有返回-1
< 7 > int lastIndexOf ( char ,int formIndex )
从formIndex往前找字符char出现位置的下标 , 没找到返回-1
< 8 > int lastIndexOf(String)
同上
< 9 > int lastIndexOf(String,int formIndex)
同上
public class Test1 {
public static void main(String[] args) {
String s="abcddeeab";
System.out.println(s.charAt(3));
System.out.println(s.indexOf('b'));
System.out.println(s.indexOf('b',4));
System.out.println(s.lastIndexOf("abc"));
System.out.println(s.indexOf("abc"));
}
}
执行结果如下:
4.转化
(1)数值和字符串转化
数字转字符 : 用String.valueOf()
字符转数字 :
int 比较特殊 , 为Integer.parseInt()
char 也比较特殊, 为Character
(包装类)
其余首字母大写, 用(类型.parse类型),例如double
Double.parseDouble()
public static void main(String[] args) {
//数字转字符
int a=12345;
String s1=String.valueOf(a);
String s2=String.valueOf(12.34);
//字符串转数字
int b1=Integer.parseInt("123");
double b2=Double.parseDouble("12.3");
}
(2)大小写转化
转大写:toUpperCase()
转小写:toLowerCase()
public static void main(String[] args) {
String s3="Abc";
String s4=s3.toUpperCase();
System.out.println(s4); //ABC
String s5=s4.toLowerCase();
System.out.println(s5); //abc
}
(3)字符串转数组
字符串转数组: 使用 toCharArray()
public static void main(String[] args) {
String s="abcd";
char[] ch=s.toCharArray();
for(int a=0;a<ch.length;a++){
System.out.print(ch[a]);
}
}
数组转字符串: 用new String()
public static void main(String[] args) {
char[] ch={'a','b','c','d'};
String s2=new String(ch);
System.out.println(s2);
} //运行结果:abcd
(4)格式化字符串,用format()
作用 : 便于修改
public static void main(String[] args) {
String s=String.format("%d-%d-%d",2024,12,11);
System.out.println(s);
}