String类
1、String的初始化赋值:
方法一:
String s=new String(“I am a student”);
方法二:
String s=”I am a student”;//(直接赋值)
2、String的比较:
注意:==判断的是地址是否相等,如果想判断值是否相等用String类的方法equals方法。
String s1=”hello”; String s2=”hello”; String s3=s2;
System.out.println(s1.equals(s2));
3、两种赋值方法的比较:
public class Demo{
public static void main(String [] args){
System.out.println("hello".equals("hello"));//”hello”是匿名对象
}
}
读懂上面的程序就好理解了。需要理解各种方法的内存结构。
String s=”hello”;//将一个堆内存空间指向了栈内存空间。
public class Demo{
public static void main(String [] args){
String s1 = "hello";
String s2 = "hello";
String s3 = "hello";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
}
}//结果为三个true.
栈 堆
s1 |
s2 |
s3 |
hello |
|
|
说明:使用直接赋值的方式可以有效的节省内存;
public class Demo{
public static void main(String [] args){
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("hello");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
}
}//这种方法会开辟三个空间
所以使用中推荐用直接赋值的方式
4、字符串的内容不可改变:
public class Demo{
public static void main(String [] args){
String s1 = "hello";
s1 = s1 + " world!";
System.out.println(s1);
}
}//结果是 : hello world!
栈 堆
字符串内容的改变实际上是改变的内存地址的引用关系
下面的代码性能很低(要断开连接10次才能完成)
public class Demo{
public static void main(String [] args){
String s1 = "hello";
//循环修改内容
for(int i=0;i<10;i++){
s1+=i;
}
System.out.println(s1);
}
}
如果非要完成这样的操作,使用StringBuffer类,专门完成这样的操作。
StringBuffer常用的方法有:append(),insert();
public class Demo{
public static void main(String [] args){
StringBuffer s1 = new StringBuffer("hello");
//循环修改内容
for(int i=0;i<10;i++){
s1.append(i);
}
System.out.println(s1);
}
}
5、String类的常用方法:
5.1:字符数组与字符串的相互转换
char[] | Converts this string to a new character array. |
public class Demo{
public static void main(String [] args){
String str1 = "hello";
char[] c = str1.toCharArray();//将字符串变为字符数组
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
System.out.println();
String str2 = new String(c);//将字符数组的全部转换为字符串
String str3 = new String(c,0,3);//将字符串的部分转换为字符串
System.out.println(str2);
System.out.println(str3);
}
}
5.2:返回字符串指定位置的值
public class Demo{
public static void main(String [] args){
String str1 = "hello";
System.out.println(str1.charAt(1));//返回e,从0位开始
}
}
5.3:字符串与(byte)字节数组的相互转换
public class Demo{
public static void main(String [] args){
String str1 = "hello";
byte[] b = str1.getBytes();//将字符串转换为byte数组
System.out.println(new String(b));//将byte数组转换为字符串
System.out.println(new String(b,1,3));//从第二个参数的位置开始转换第三个参数个
}
}
5.4:取字符串的长度
字符串名称.length();//注意后边有括号
有的时候容易与字符数组的用混(注:字符数组的长度: 字符数组名.length;无括号)
5.5:查找指定字符串是否存在
Indexof();
方法的返回值是一个数,如果找到则返回指定字符串的位置,如果没找到则返回-1.
public class Demo{
public static void main(String [] args){
String str1 = "abcdefcdw";
System.out.println(str1.indexOf("c"));//查找字符串"c"所在的位置
System.out.println(str1.indexOf("c",3));//从第四个位置开始往后找字符串的位置
System.out.println(str1.indexOf("x"));//没有找到则返回-1
}
}
5.6:除去字符串中的空格
public class Demo{
public static void main(String [] args){
String str1 = " hello ";
System.out.println(str1.trim());//除去字符串左右的空格
}
}
5.7:字符串的截取
public class Demo{
public static void main(String [] args){
String str1 = "hello world";
System.out.println(str1.substring(6));//从第7个位置开始截取
System.out.println(str1.substring(0,5));//截取0到5之间的内容
}
}
5.8:拆分字符串
按指定字符串拆分一个字符串。
public class Demo{
public static void main(String [] args){
String str1 = "hello world";
String[] str2 = str1.split(" ");//按空格将字符数组拆分
for(int i=0;i<str2.length;i++)
System.out.println(str2[i]);
}
}
5.9:字符串大小写转换
public class Demo{
public static void main(String [] args){
String str1 = "hello world";
System.out.println(str1.toUpperCase());//转大写
System.out.println(str1.toLowerCase());//转小写
}
}
5.10:判断是否以指定字符串开头或结尾
public class Demo{
public static void main(String [] args){
String str1 = "**hello world";
String str2 = "hello world**";
if(str1.startsWith("**")){System.out.println("以**开头");}
if(str2.endsWith("**")){System.out.println("以**结尾");}
}
}
5.11:不区分大小写的操作
public class Demo{
public static void main(String [] args){
String str1 = "hello world";
String str2 = "HELLO WORLD";
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
}
}
5.12:字符串的替换
public class Demo{
public static void main(String [] args){
String str1 = "hello world";
String str2 = str1.replace("l","x");//将字符串中的“l”全部转换为"x";
System.out.println(str2);
}
}