java——string类

String实例化:

字符串的比较:

public class Main {
    public static  void main(String args []) {
        String stra = "hello";
        String strb = new String("hello");
        String strc = strb;
        System.out.println(stra==strb);  //false
        System.out.println(stra==strb);  //false
        System.out.println(strb==strc);  //true

        System.out.println(stra.equals(strb));  //true
        System.out.println(stra.equals(strc));  //true
        System.out.println(strb.equals(strc));  //true

    }
}

在字符串相等判断中,“==”与“equals()”的区别:

“==”是java提供的关系运算符,主要功能是进行数值相等判断,如果用在String上则表示内存地址数值的比较。

“equals()”是由String提供的一种方法,专门负责进行字符串内容的比较。

采用直接赋值的String类对象内存地址完全相同:

不会产生垃圾空间,而且可以自动入池。

public class Main {
    public static  void main(String args []) {
        String stra = "hello";
        String strb = "hello";
        String strc = "hello";

        System.out.println(stra==strb);  //true
        System.out.println(stra==strb);  //true
        System.out.println(strb==strc);  //true
    }
}

共享设计模式:

在jvm底层会存在一个对象池(不一定只保存String对象),当代码中使用直接赋值的方式定义一个String类对象时,会将此字符串对象使用的匿名对象入池保存,若后续使用匿名对象定义String类对象时,并设置同样的值,将不会开辟新的堆内存空间,而是对已有的对象进行引用分配。

采用构造方法实例化:

一定要使用关键字new,会开辟两块堆内存空间(其中一块成为垃圾空间)。

构造方法定义的String类对象,内容不会保存在对象池中(用new开辟的新内存)。如果希望开辟的新内存数据也可以进行对象池的保存,可以使用String定义的手工入池的方法

public class Main {
    public static  void main(String args []) {
        String stra = new String("hello").intern();      //使用构造方法定义了新的内存空间,而后入池
        String strb = "hello";

        System.out.println(stra==strb);  //true
    }
}

String两种实例化方式的区别:

直接赋值(String str="字符串";):只会开辟一块堆内存空间,并且会自动保存在对象池之中,以供下次重复使用;

构造方法(String str=new String("字符串");):会开辟两块堆内存空间,且不会自动入池。

字符串内容一旦声明不可改变,String类对象内容的改变是依靠引用关系的变更实现的

String类方法:

字符串转化:

取出指定索引字符:

public class Main {
    public static  void main(String args []) {
        String stra = "hello";
        char a=stra.charAt(0);
        System.out.println(a);
    }
}

字符串与数组的转换:

public class Main {
    public static  void main(String args []) {
        String stra = "hello";
        char date[] = stra.toCharArray();   //字符串变数组
        for (int x = 0; x < date.length; x++) {
            date [x]-=32;                     //小写转大写
            System.out.println(date[x]);
        }
        System.out.println(new String(date));    //数组变字符串
    }
}

字符串与字节数组的转化

public class  Main{
    public  static  void main(String srgs []){
        String str="hello";
        byte [] date=str.getBytes();         //将字符串转化为字节数组
        for (int x=0;x<date.length;x++){
            System.out.print(date[x]+",");           //104,101,108,108,111,
        }
        String stra=new String(date);         //将字节数组转化为字符串
        System.out.println(stra);                  //hello
    }
}

字符串的比较:

判断是否相同

public boolean equals(Object anObject)
public boolean equalsIgnoreCase(String anotherString)
public class Main {
    public static  void main(String args []) {
        String stra = "hello";
        String strb = "Hello";
        System.out.println(stra.equals(strb));           //false   区分大小写
        System.out.println(stra.equalsIgnoreCase(strb));  //true   不区分大小写
    }
}
判断大小关系
public int compareTo(String anotherString)
public class Main {
    public static  void main(String args []) {
        String stra = "ab";
        String strb = "AB";
        if (stra.compareTo(strb)>0){             //只有String才具有大小关系判断
            System.out.println("stra大于strb");           //32
        //System.out.println(stra.equalsIgnoreCase(strb));  //true   不区分大小写
        }
    }
}

字符串查找

indexo方法

查找索引,-1为不在字符串内

public class Main {
    public static  void main(String args []) {
        String str = "helloworldh";
        System.out.println(str.indexOf("wo"));        //字符串中第一个“wo”的索引     5
        System.out.println(str.indexOf("wo",5));   //字符串中第5个开始查找"wo"的索引       5
        System.out.println(str.lastIndexOf("h"));         //查找字符串中的“h”        10
    }
}

contains方法

判断是否包含在内

public class Main {
    public static  void main(String args []) {
        String str = "helloworld";
        System.out.println(str.contains("hello"));
    }
}

startWith,endWith方法

是否以**开头

public class Main {
    public static  void main(String args []) {
        String str = "helloworld";
        System.out.println(str.startsWith("hello"));  //true
        System.out.println(str.startsWith("world",5));  //true   从第5个为开始
        System.out.println(str.startsWith("ello"));  //false
    }
}

字符串的替换

 

public class Main {
    public static  void main(String args []) {
        String str = "helloworld";
        System.out.println(str.replaceAll("l","a"));    //heaaoworad
        System.out.println(str.replaceFirst("l","a"));    // healoworld
    }
}

字符串截取

public class Main {
    public static  void main(String args []) {
        String str = "helloworld";
        System.out.println(str.substring(5));    //world         从第五个开始截取
        System.out.println(str.substring(0,5));    // hello      从第一个开始截取
    }
}

字符串拆分

进行全部拆分:

public class Main {
    public static  void main(String args []) {
        String str = "hello world java";
        String data [] = str.split(" ");   //以空格拆分
        for (int x=0;x<data.length;x++){
            System.out.println(data[x]);
        }
        String data1 [] = str.split(" ",2);   //以空格拆分,拆分为两部分
        for (int x=0;x<data1.length;x++){
            System.out.println(data1[x]);
        }
        String stra="192.168.43.29";
        String data2 [] =stra.split("\\.");     //敏感字符需要用“\\”转义
        for (int x=0;x<data2.length;x++){
            System.out.println(data2[x]);
        }
    }
}

 

猜你喜欢

转载自blog.csdn.net/qq_36230524/article/details/89785217