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!
        }
相关推荐
凤凰院凶涛QAQ1 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
掘金_答案2 小时前
上线那天,一个 ConcurrentHashMap 差点送走我的 AI 客服——3 天排查 JVM 血泪史
java·后端·架构
猿与禅2 小时前
CosId 分布式 ID 生成器完全教程:从架构原理到生产落地
java·shardingsphere·雪花算法·分布式id·高性能·cosid·号段模式
MindUp2 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
NG4772 小时前
【软件设计与体系结构】-策略设计模式
java·设计模式·软件工程
程序员天天困3 小时前
优雅记录操作日志:从注解到 SpEL 的全链路实践与开源方案对比
java·后端
战血石LoveYY3 小时前
Integer类型超限,导致数据异常
java·算法
乐观的Terry5 小时前
1、为什么要自己造发布系统
java·spring boot·后端·spring cloud·架构
兰令水5 小时前
hot100【acm版】【2026.7.13打卡-java版本】
java·开发语言·数据结构·算法·leetcode·面试
嗝屁小孩纸5 小时前
Maven版本升级&Nexus推送极简执行步骤
java·elasticsearch·maven