• Day33:String类及其常用方法详解


    String类

    1.1 String类概述

    Java中字符串属于对象,String类用于创建和操作字符串。

    最简单的字符串创建:直接通过String创建

    String str="工地佬";
    

    利用String构造器创建字符串

    String str=new String("工地佬");
    

    字符串的特点:

    • String对象是不可变的。字符串(常量)一旦被创建,其内容是不可变的。

    • String创建的字符串对象是存储在字符串池中的,是可以共享的。

    • new创建的字符串对象是存储在堆内存中的

    String str="工地佬";
           str="工具人";
    System.out.println(str);
    
    工具人//很明显,str的地址指向指向了常量池中的"工具人"
    

    我们对上面代码进行内存分析

    第一行代码在常量池中创建的面值为"工地佬"字符串且赋值给str,str指向常量池中的"工地佬";

    第二行代码 str="工具人";程序并没有将"工地佬"改成"工具人",而是在常量池中创建了新的字符串”工具人“,并将引用赋值给了str,此时原来的字符串对象在常量池中便失去了指向,但依然存在。


    在Java中,Java虚拟机为了节约内存,将具有相同序列的字符串字面值使用同一个对象!

    String s1="工地佬";                     //String直接创建
    String s2=new String("工地佬");         //new创建
    String s3=s1;                          //引用相同
    String s4=new String("工地佬");         //new创建
    

    //== 在引用类型使用时判断的是引用地址是否相同
    System.out.println(s1==s2);//s1是创建在常量池中,s2是创建在堆内存中,两者引用不同,自然也就不会相同
    System.out.println(s1==s3);//s1是创建在常量池中,而s1的地址引用赋值给了s3,则两者引用地址相同
    System.out.println(s2==s4);//s2、s4均由new产生,两者开辟了不同的内存空间,则地址不同,所以不相等
    
    false
    true
    false
    

    1.2 String类的常用方法

    • 返回字符串长度

      public int length();非静态方法,需要实例化才能使用

    public class Test{
        public static void main(String[] args){
            String a="java";
            System.out.println(a.length());//length方法为非静态方法,需要通过对象来调用
        }
    }
    
    4
    
    • 获取指定下标的字符

    public char charAt(int index);非静态方法,需要实例化才能使用

    public class Test{
        public static void main(String[] args){
            String a="java";
            System.out.println(a.charAt(0));
        }
    }
    
    j
    
    • 判断字符串中是否含有指定的字符串

    public boolean contains();非静态方法,实例化后可用;判断对象中是否含有字符串str,并返回布尔值

    public class Test{
        public static void main(String[] args){
            String a="java";
            System.out.println(a.caontains("j"));
        }
    }
    
    true
    
    • 将字符串转为数组

    public char[] toCharArray(String str); 将字符串以数组的形式返回

    public class Test{
        public static void main(String[] args){
            String a="工地佬";
            System.out.prtintln(a.toCharArray());
            //此时已经是数组,我们可以调用数组的打印功能,让控制台输出的数组更加规范
            System.out.println(Arrays.toString(b.toCharArray()));
        }
    }
    
    工地佬
    [工,地,佬]
    
    • 搜索子字符串在字符串中的首次出现位置的下标值

    public int indexOf(String str); 返回子字符串在对象中首次出现的下标值;如果对象中不含有待搜索的字符串则会返回-1

    public class Test{
        public static void main(String[] args){
            String a="中交第二航务工程局建筑工程有限公司";
            System.out.println(a.indexOf("中"));
            System.out.println(a.indexOf("佬"));
            
            //String中关于indexOf还有重载的方法
            //可以定义从对象字符串中哪一个下标开始进行索引
            System.out.println(a.indexOf("工",7));//意为从对象字符串值中下标为7的字符开始索引"工",并返回索引成功后的下标
        }
    }
    
    1
    -1
    11
    
    • 搜索子字符串在对象中出现的最后一次的下标值

    public int lastIndexOf(String str);返回子字符串在对象中最后一次出现的下标值;如果对象中不含有待搜索的字符串则会返回-1

    public class Test{
        public static void main(String[] args){
            String a="中交第二航务工程局";
            System.out.println(a.lastIndexOf("中"));
            //同样,若待查找的子字符串在对象中并不存在的话,将返回-1
            System.out.println(a.lastIndexOf("佬"));
        }
    }
    
    1
    -1
    
    • 去除字符串前后的空格

    public String trim(); 将字符串前后的空格去除掉后返回; (原始字符串没有被改变,返回的是一个新的字符串)

    public class Test{
        public static void main(String[] args){
            String a="   中交   第二航务  工程局   ";
            System.out.println(a.trim());
        }
    }
    
    中交   第二航务  工程局
    
    • 大小写转换

    public String toUpperCase(); 将字符串中的小写全部转回大写,并返回

    public String toLowerCase(); 将字符串中的大写全部转回小写,并返回

    public class Test{
        public static void main(String[] args){
            String a="java IS THE best Language";
            System.out.println(a.toUpperCase());
            System.out.println(a.toLowerCase());
        }
    }
    
    JAVA IS THE BEST LANGUAGE
    java is the best language
    
    • 判断字符串是否以子字符串为结尾

    public boolean endsWith(String str);判断字符串中是否已str为结尾,返回布尔值

    public boolean startsWith(String str);判断字符串中是否已str为开头,返回布尔值

    public class Test{
        public static void main(String[] args){
            String a="java is the best language in the world!"
            System.out.println(a.endsWith("in"));
            System.out.println(a.startsWith("in"));
        }
    }
    
    false
    false
    
    • 将字符串中的目标字符串替换成新的字符串

    public String replace(char oldChar,char newChar);

    public class Test{
        public static void main(String[] args){
            String a="java is the best language in the world!";
            System.out.println(a.replace("in","all over"));
        }
    }
    
    java is the best language all over the world!
    
    • 根据给定条件对字符串进行拆分

    public String[] split(String str); 将字符串按照给定的str为拆分信号进行拆分,并重新组合成字符串数组返回

    public class Test{
        public static void main(String[] args){
            String a="java is the best language in the world!";
            System.out.println(Array.toString(a.split(" ")));
            String b="工 地 佬 ,是 牛 马";
            //如果字符串中含有空格,逗号,该如何拆分字符串
            //[ ,] 表示选择"空格"或者","为分隔符进行拆分
            System.out.println(Array.toString(b.split("[ ,]")));
            
            //如果字符串中空格或者逗号的个数有多个,应该怎么拆分得到有效字符串呢
            //[ ,]+   表示选择一个或者多个"空格"或者","为分隔符进行拆分
            String b="工 地   佬   ,是 牛   马";
            System.out.println(Array.toString(b.split("[ ,]+")));
        }
    }
    
    [java, is, the, best, language, in, the, world]
    [java, is, the, best, language, in, the, world]
    [java, is, the, best, language, in, the, world]
    
    • 提取字符串中子字符串

    public String subString(int beginIndex); 返回从指定下标到结尾的子字符串

    重载方法:public String subString(int beginIdex,int endIndex);返回指定下标区域的子字符串并返回

    public class Test{
        public static void main(String[] args){
            String a="java is a computer language!"
            String b=a.subString(5);
            System.out.println(b);
            System.out.println(b.subString(5,10))
        }
    }
    
    is a computer language!
    is a 
    
    • 比较字符串内容是否相等

    public boolean equals(Object o); String类中的equals方法是重写了Object中的equals方法,其功能为比较两者内容是否一致

    public class Test{
        public static void main(String[] args){
            String a="bba";
            String b=new String("bba");
            System.out.println(a.equals(b));//String类中的equals已经重写,功能为比较两个对象的内容是否一致,并非地址
        }
    }
    
    true
    

    public int compareTo(String str); 比较两个字符串的Unicode码的差值

    public class Test{
        public static void main(String[] args){
            String a="bba";
            String b="background";
            System.out.println(a.compareTo(b));//两者比较对应字符的Unicode码差值,出现差值后,结束比较
            
            //特殊情况  两个字符串面值属于包含关系,那么比较的则是字符串长度
            String c="back";
            System.out.println(b.compareTo(c));//b包含c,两者比较则是比较字符串长度
            
        }
    }
    
    1
    6
    
  • 相关阅读:
    AMD CPU 虚拟机安装 macos 系统的各虚拟机系统对比
    好的内容回复区
    基于SSM的学院学生论坛系统的设计与实现
    信号量的使用
    C语言 指针进阶 壹
    加密软件VMProtect教程:使用脚本-功能
    springboot心理咨询管理系统
    Langchain-Chatchat-win10本地安装部署成功笔记(CPU)
    k8s编程operator——client-go中的informer
    【ES6】require、export和import的用法
  • 原文地址:https://www.cnblogs.com/CQliuwei/p/16961738.html