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中,可变长字符串主要有两种实现:StringBuffer 和 StringBuilder。两者均用于动态字符串操作,但在性能和线程安全性上有所不同。
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()); // 空字符串
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2026年06月03日 04时27分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php支付宝手机网页支付类实例
2023-03-01
PHP改变数组key值的方法
2023-03-01
php教程之php空白页的原因及解决方法
2023-03-01
PHP数据库操作
2023-03-01
PHP数据文件过大,导致PHP加速器eaccelerator在PHP5.2版本下崩溃
2023-03-01
RabbitMQ - 死信、TTL原理、延迟队列安装和配置
2023-03-01
PHP数据访问的多重查询(租房子查询)
2023-03-01
RabbitMQ - 如保证消息的可靠性?(消息确认、消息持久化、失败重试机制)
2023-03-01
RabbitMQ - 基于 SpringAMQP 带你实现五种消息队列模型
2023-03-01
php数组函数分析--array_column
2023-03-01
php数组去重复数据的小例子
2023-03-01
php数组实现:哈希 +双向链表
2023-03-01
PHP数组排序函数array_multisort()函数详解(二)
2023-03-01
php数组的几个函数和超全局变量
2023-03-01
PHP文件上传详解
2023-03-01
PHP文件锁
2023-03-01
php文本框输入制定文本,php – 当用户没有向文本框输入任何内容时...
2023-03-01
PHP时间戳和日期相互转换操作总结
2023-03-01
php时间戳知识点,php 时间戳函数总结与示例
2023-03-01
php更新数据库失败,php – 无法更新MySQL数据库
2023-03-01