String类

String类的路径java.lang.String

创建字符串

java 复制代码
String str = "Hello World";

String类下的方法

  • toString()

    返回字符串本身,返回值为 String 类型。

  • concat()

    将两个字符串连接在一起

    java 复制代码
        public static void main(String[] args) {
            String str1 = "Hello";
            String str2 = " World!";
            String concatstr = str1.concat(str2);
            System.out.println(concatstr); // Hello World!
        }
  • equals()

    比较两个字符串的值

    java 复制代码
        public static void main(String[] args) {
            String str1 = "Hello";
            String str2 = "Hello";
            Boolean result = str1.equals(str2);
            System.out.println(result); // true
        }
  • getBytes()

    将字符串转换为字节数组

    java 复制代码
        public static void main(String[] args) {
            String str1 = "Hello";
            byte[] str1_byte = str1.getBytes();
            System.out.println(Arrays.toString(str1_byte)); // [72, 101, 108, 108, 111]
        } 
  • indexOf()

    返回字符串中指定字符或字符串的位置,如果没有找到指定的字符或者字符串,返回-1。

    java 复制代码
        public static void main(String[] args) {
            String str1 = "Hello";
            int ret1 = str1.indexOf("H");
            int ret2 = str1.indexOf("e");
            int ret3 = str1.indexOf("a");
            System.out.println("str1: " + ret1); // 0
            System.out.println("str2: " + ret2); // 1
            System.out.println("str3: " + ret3); // -1
        }
  • length()

    返回字符串字符序列的长度(包含空格)。

java 复制代码
    public static void main(String[] args) {
        String str1 = "Hello ";
        int length = str1.length();
        System.out.println(length); // 6
    }
  • isEmpty()
    判断字符串是否为空,返回值为 boolean 类型(根据length来判断)。
java 复制代码
    public static void main(String[] args) {
        String str1 = "Hello ";
        Boolean isempty = str1.isEmpty();
        System.out.println(isempty); // false
    }
  • substring(...)
    这个方法有两个重载方法substring(int beginIndex)substring(int beginIndex, int endIndex)
    substring(int beginIndex)返回从 beginIndex 开始到原字符串末尾的子字符串。
    substring(int beginIndex, int endIndex)返回从 beginIndex 开始到 endIndex 结束(不包括 endIndex)的子字符串。
    substring() 方法不会修改原始字符串,它返回的是一个新的字符串对象。

    java 复制代码
        public static void main(String[] args) {
            String str1 = "Hello World!";
            String sub1 =  str1.substring(6);
            System.out.println(sub1); // World!
            System.out.println(str1.length()); // 12
            String sub2 =  str1.substring(6,12);
            System.out.println(sub2); // World!
        }
  • replace()
    public String replace(CharSequence target, CharSequence replacement)把旧的字符或字符序列替换为新的字符或字符序列。

    java 复制代码
        public static void main(String[] args) {
            String str1 = "Hello World!";
            String str2 = "Hello World!".replace("Hello","NiHao");
            System.out.println(str2); // NiHao World!
        }
相关推荐
曹牧1 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
爬山算法2 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7252 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎2 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄2 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
忆~遂愿2 小时前
ops-cv 算子库深度解析:面向视觉任务的硬件优化与数据布局(NCHW/NHWC)策略
java·大数据·linux·人工智能
小韩学长yyds2 小时前
Java序列化避坑指南:明确这4种场景,再也不盲目实现Serializable
java·序列化
仟濹3 小时前
【Java基础】多态 | 打卡day2
java·开发语言
Re.不晚3 小时前
JAVA进阶之路——无奖问答挑战2
java·开发语言
Ro Jace3 小时前
计算机专业基础教材
java·开发语言