String类
发布日期:2021-04-30 21:03:58 浏览次数:87 分类:精选文章

本文共 3037 字,大约阅读时间需要 10 分钟。

Java字符串操作及可变长字符串分析

1. 字符串池及常量特性

在Java中,字符串是常量,创建后不可变。字符串池(String Pool)用于存储字符串常量,允许多个引用共享同一内存。例如:

String name = "hello"; // "hello" 存储在字符串池中String name2 = "zhangsan"; // 给字符串赋值时,新建内存空间

2. 字符串操作方法

2.1 length()方法
String content = "你好,java!";System.out.println(content.length()); // 8
2.2 charAt()方法
String content = "你好,java!";System.out.println(content.charAt(content.length() - 1)); // '!'
2.3 contains()方法
String content = "你好,java!";System.out.println(content.contains("java")); // true
2.4 toCharArray()方法
String content = "你好,java!";System.out.println(Arrays.toString(content.toCharArray())); // [你, 好, ,, j, a, v, a, !]
2.5 indexOf()方法
String content = "a你好,java!";System.out.println(content.indexOf("a")); // 0System.out.println(content.indexOf("a", 2)); // 5
2.6 lastIndexOf()方法
String content = "a你好,java!";System.out.println(content.lastIndexOf("a")); // 7
2.7 trim()方法
String content = "   hello world   ";System.out.println(content.trim()); // hello world
2.8 toUpperCase()方法
String content = "hello world";System.out.println(content.toUpperCase()); // HELLO WORLD
2.9 toLowerCase()方法
String content = "HELLO WORLD";System.out.println(content.toLowerCase()); // hello world
2.10 endWith()方法
String content = "你好,java!";System.out.println(content.endWith("a")); // false
2.11 startWith()方法
String content = "你好,java!";System.out.println(content.startWith("你好")); // true
2.12 replace()方法
String content = "你好,java!";System.out.println(content.replace("你好", "hello")); // hello,java
2.13 split()方法
String content = "hello    world,hello java";String[] arr = content.split("[ ,]+");for (String x : arr) {    System.out.println(x);}
2.14 equalsIgnoreCase()方法
String s1 = "java";String s2 = "JAVA";System.out.println(s1.equalsIgnoreCase(s2)); // true
2.15 compare()方法
String s1 = "abc";String s2 = "xyz";System.out.println(s1.compare(s2)); // -23String s3 = "abc";String s4 = "abcxyz";System.out.println(s1.compare(s4)); // -3

3. 字符串操作实例

假设有字符串 str = "this is a text",可以执行以下操作:

  • 提取单词

    String[] arr = str.split(" ");
  • 替换单词

    System.out.println(str.replace("text", "practice"));
  • 插入字符

    System.out.println(str.replace("text", "easy text"));
  • 修改首字母

    for (int i = 0; i < arr.length; i++) {    char first = arr[i].charAt(0);    char upperfirst = Character.toUpperCase(first);    String newStr = upperfirst + arr[i].substring(1);    System.out.println(newStr);}
  • 4. 可变长字符串

    在Java中,可变长字符串主要有两种实现:StringBufferStringBuilder。两者均用于动态字符串操作,但在性能和线程安全性上有所不同。

    4.1 StringBuffer(线程安全,但效率较低)
    StringBuffer sb = new StringBuffer();sb.append("hello,java");sb.append("hello,world");System.out.println(sb.toString()); // hello,javahello,world
    4.2 StringBuilder(线程不安全,效率较高)
    StringBuilder sb = new StringBuilder();sb.insert(0, "The First");System.out.println(sb.toString()); // The Firsthello,javahello,world
    4.3 操作示例
    sb.replace(0, 3, "I am");System.out.println(sb.toString()); // I am Firsthello,javahello,world
    sb.delete(0, 5);System.out.println(sb.toString()); // Firsthello,javahello,world
    sb.delete(0, sb.length());System.out.println(sb.toString()); // 空字符串
    上一篇:8-Spring Boot消息服务
    下一篇:厚积薄发打卡Day24 :狂神说Java之注解与反射<全网最全(代码+笔记)>

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2026年06月03日 04时27分07秒