• 《Java基础知识》Java字符串详解


    本文内容:

    String类的概述

    String类的使用步骤

    String类的常用方法

    本文目的:

    能够使用String类的构造方法创建字符串对象

    能够明确String类的构造方法创建对象,和直接赋值创建字符串对象的区别

    能够使用文档查询String类的判断方法

    能够使用文档查询String类的获取方法

    能够使用文档查询String类的转化方法

    一、String类

    概述java.lang.String 类代表字符串。Java程序中所有的字符串文字(比如"abc")都可以被看成是实现此类的实例。类String中包括用在检查每一个字符串的方法,比如用于比较字符串,搜索字符串,提取字符串以及创建具有翻译为大写或者是小写的所有字符的字符串副本。

    特点(1)字符串是不变的,字符串的值在创建以后是不可以被更改的

    案例:

    public class var {
        public static void main(String[] args) {
            String str = "abc";
            str += "d";
            System.out.println(str);
        }
    }

    运行结果:

    特点(2)因为String对象是不可以变换的,所以它们才可以被共享。

    public class var {
        public static void main(String[] args) {
            //内存中只有一个“abc”被创建。
            String s1 = "abc";
            String s2 = "abc";
        }
    }

    特点(3)"abc"等于char[ ] data={′a′,′b′,′c′}

    public class var {
        public static void main(String[] args) {
            char[] data={'a','b','c'};
            String str = new String(data);
            System.out.println(str);
        }
    }

    运行结果:

    3. 使用步骤

     查看类

    java.lang.String:此类不需要再导入

    public String( ):初始化新创建的String对象,可以让它表示空字符序列

    public String(char[ ] value):通过当前参数当中的字符数组来构造新的String

    public String(byte[ ] bytes):通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String

    构造举例,代码如下所示:

    public class var {
        public static void main(String[] args) {
            //无惨构造
            String str0 = new String();
            //通过字符数组构造
            char[] data={'a','b','c'};
            String str1 = new String(data);
    
            //通过字节数组构造
            byte[] bytes={97,98,99};
            String str2 = new String(bytes);
        }
    }

    4.常用的方法

    (1)判断功能的方法

    案例:

    public class var {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = "hello";
            String s3 = "HELLO";
            System.out.println("------------通过equals比较-----------------");
            if(s1.equals(s2)){
                System.out.println("s1.equals(s2): true");
            }
            if(!s1.equals(s3)){
                System.out.println("s1.equals(s3): false");
            }
            System.out.println("------------通过equalsIgnoreCase比较-----------------");
            if(s1.equalsIgnoreCase(s2)){
                System.out.println("s1.equals(s2): true");
            }
            if(s1.equalsIgnoreCase(s3)){
                System.out.println("s1.equals(s3): true");
            }
        }
    }

    运行结果:

    (2)获取功能的方法:

    public class var {
        public static void main(String[] args) {
            String s1 = "hello";
            //获取字符串长度
            System.out.println("字符串长度:"+s1.length());
            //拼接字符串
            String s2 = s1.concat("world");
            System.out.println("字符串s1拼上world后:"+s2);
            //获取指定位置元素
            char c1= s1.charAt(0);
            System.out.println("字符串s1第一位:"+c1);
            //获取字符串中出现指定元素的位置
            int i = s1.indexOf("e");
            System.out.println("字符串s1中‘e’所在位置:"+i);
            //截取字符串
            String s3 = s1.substring(0,2);
            System.out.println("截取字符串s1中1到2的元素:"+s3);
        }
    }

    运行结果:

    (3)转换功能的方法:

    public class var {
        public static void main(String[] args) {
            String s1 = "hello";
            //字符串转成字符数组
            char[] arr = s1.toCharArray();
            for (int i = 0; i < arr.length ; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
            //转换成字节
            byte[] b1 = s1.getBytes();
            for (int i = 0; i < b1.length ; i++) {
                System.out.print(b1[i] + " ");
            }
            System.out.println();
            //字符替换
            String s2 = s1.replace("ll","LL");
            System.out.println("替换后的字符串为"+s2);
        }
    }

    运行结果:

    (4)分割功能的方法:

    public class var {
        public static void main(String[] args) {
            String s1 = "hello,world";
            String[] arr = s1.split(",");
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }

    运行结果:

    参考:https://baijiahao.baidu.com/s?id=1622780499548219487&wfr=spider&for=pc

    This moment will nap, you will have a dream; But this moment study,you will interpret a dream.
  • 相关阅读:
    git修改文件名大小写的方法。
    VC中常用的宏
    spring cloud Zuul 多层拦截 --- 心得
    Zuul网关 @EnableZuulProxy 和 @EnableZuulServer 的区别
    jave 数据类型 float 的 正确赋值
    spring cloud bus 消息总线 动态刷新配置文件 【actuator 与 RabbitMQ配合完成】
    RabbitMQ 消息中间件 的下载与安装【window10】
    spring cloud --- 使用 actuator 热更新【刷新】单机配置文件
    spring cloud 与spring boot的版本对应总结
    spring cloud feign 报错 feign.FeignException$MethodNotAllowed: status 405 reading 解决
  • 原文地址:https://www.cnblogs.com/jssj/p/11145905.html
Copyright © 2020-2023  润新知