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!
        }
相关推荐
这周也會开心13 分钟前
云服务器安装JDK、Tomcat、MySQL
java·服务器·tomcat
hrrrrb1 小时前
【Spring Security】Spring Security 概念
java·数据库·spring
小信丶1 小时前
Spring 中解决 “Could not autowire. There is more than one bean of type“ 错误
java·spring
周杰伦_Jay2 小时前
【Java虚拟机(JVM)全面解析】从原理到面试实战、JVM故障处理、类加载、内存区域、垃圾回收
java·jvm
程序员小凯6 小时前
Spring Boot测试框架详解
java·spring boot·后端
豐儀麟阁贵6 小时前
基本数据类型
java·算法
_extraordinary_6 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
程序员 Harry7 小时前
深度解析:使用ZIP流式读取大型PPTX文件的最佳实践
java
wxweven7 小时前
校招面试官揭秘:我们到底在寻找什么样的技术人才?
java·面试·校招
陈陈爱java8 小时前
新知识点背诵
java