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!
        }
相关推荐
别动我齐刘海3 小时前
C++ 工程化学习
java·linux·前端·c++·学习·机器学习·机器人
code2cat4 小时前
Java进阶篇之AtomicLongFieldUpdater:把累计额度留在对象字段中
java·cas·并发编程
WeiXin_DZbishe4 小时前
基于springboot城市公交实时运营与调度系统-计算机毕设 附源码【课程设计】66173
java·vue.js·spring boot·后端·python·php·课程设计
泡茶喝茶写代码4 小时前
A股量化数据工程:从 REST 接口到策略信号(第 18 篇):放量缩量异动扫描
java·python·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
wewowilling5 小时前
认识微服务
java·运维·微服务·springcloud
代码什么用5 小时前
Java Web 7-10
java·servlet
蜗牛互联网7 小时前
GPT-6 与 Intelligent UI:从回答到可审计操作的产品迁移
java·人工智能·后端
SL_staff7 小时前
运维救火实录:钉钉考勤API变更引发工资计算异常,我们用JVS-Logic可视化逻辑引擎实现分钟级热修复
java·mysql·全栈
SunnyDays10117 小时前
Java 查找并高亮 Word 文字:精确查找及正则表达式模糊查找
java·查找文字·word 文档·高亮文字·正则匹配查找·模糊查找