【Javase基础语法】String类常见的字符串操作方法

引言

个人主页:努力学编程'-CSDN博客

学习领域:C语言进阶,javase基础语法

每日鸡汤:爱意随风起,人生不言弃

字符串的定义:

复制代码
public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello bit";
System.out.println(s1);
// 直接newString对象
String s2 = new String("hello bit");
System.out.println(s1);
// 使用字符数组进行构造
char[] array = {'h','e','l','l','o','b','i','t'};
String s3 = new String(array);
System.out.println(s1);
}

String对象的比较

这里介绍四种字符串的比较方式,这四种不同的方式,有着各自不同的比较方式,这里希望大家可以把这四种都牢记下来。

1.==字符串的比较

复制代码
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==比较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // true
}

总的来说就是==比较的是否是同一种对象的引用 ,当你new一个字符串时在堆里创建一个新的地址,所以即使两个字符串存放的内容是一致的,但是他们属于不同的地址,所以==会输出false

2.equals

复制代码
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
}

equals在String类中,是用来比较两个字符串的内容是否相同,即只是比较字符串的内容,不管字符串的地址。

3.compareTo

复制代码
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
}

此字符串是用来比较字符串的大小的方法,当运行结果>0时,s1>s2,当结果<0时,s1<s2.

有点类似于C语言中的strcmp字符串比较方法,可以对比记忆。

4.compareToIgnoreCase

复制代码
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("ABc");
String s4 = new String("abcdef");
System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
}

和compareTo很像,区别是compareToIgnoreCase()比较时会忽略字母的大小写格式,只比较字符是否相等。

字符串的查找

复制代码
public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10)); // 3

字符串的转化

1.数字和字符串的转化

复制代码
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("Hanmeimei", 18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("=================================");
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}

字符串转数字,和数字转字符是非常重要的String类方法在我们后面学习数据结构时可能会经常用到。

2.大小写转化

复制代码
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}

3.字符串转数组

复制代码
public static void main(String[] args) {
String s = "hello";
// 字符串转数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
System.out.println();
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}

我们经常把数组转字符串,将数组以字符串的形式打印。

字符串的替换

1.replaceAll(String regex, String replacement)

复制代码
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));//这里是把字符串中的'l'全部替换为'-'
System.out.println(str.replaceFirst("l", "_"));//这里是只替换第一次出现'l',改为'-'

字符串拆分

1.split()

复制代码
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}//这里就会产生四个字符串数组然后用for-each打印出来

字符串截取

1.substring(int beginIndex)

复制代码
String str = "helloworld" ;
System.out.println(str.substring(5));//从索引5开始截取直到最后
System.out.println(str.substring(0, 5));//这个是左开右闭截取字符串

其他字符串操作

1.trim()

去掉字符串中的左右空格 , 保留中间空格

复制代码
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
//这里输出的是hello world

字符串的不可变性

字符串其实不能直接进行修改,当我们直接对字符串进行修改时,内存里会创建一个新的字符串,并没有在原字符串的基础上进行修改

复制代码
public static void main(String[] args) {
    String str=new String(abcd);
    for(int i=0;i<1000;i++){
    str+=i;
    System.out.println(str);
//你可以思考一下,这个str会在内存里面创建多少个字符串

StringBuilder和StringBuffer

为了解决上面的问题我们使用与String相似的两个类StringBuilder和StringBuffer进行字符串的操作。这两个类来修改字符串会直接在源字符串上进行修改。

复制代码
public static void main5(String[] args) {
        String str="hello";
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(str);
        stringBuilder.append("world");
        stringBuilder.append(1);
        System.out.println(stringBuilder);
//        当然你字符串都可以在后面加int形数

        String str2="hello";
        StringBuilder stringBuilder1=new StringBuilder();
        stringBuilder1.append(str2);
        for(int i=0;i<1000;i++){
           System.out.println(stringBuilder1.append(i));
//           StringBuilder可以直接对字符串进行操作它的效率就要高得多
        }
    }
相关推荐
旋生万物2 分钟前
螺旋角分布的功率谱分析:多窗谱 + FDR 校正
开发语言·python
沫璃染墨13 分钟前
《从零入门Linux系统篇(五十七):线程篇·十——生产者消费者模型进阶:从环形缓冲区到POSIX信号量》
linux·运维·服务器·开发语言·c++·系统架构·信号处理
时间的拾荒人18 分钟前
Qt 窗口系统详解:从菜单栏到对话框的完整指南
开发语言·qt·面试
专业程序开发源21 分钟前
SSM校园拍摄交流服务平台36936-计算机课程设计、毕业设计
java·spring boot·后端·python·elasticsearch·php·课程设计
用户0942485680322 分钟前
第23章:OpenJDK逃逸分析、标量替换与锁优化
java·jvm
wuminyu25 分钟前
LockStack在虚拟线程Mount和Unmount拷贝过程剖析
java·linux·c语言·jvm·c++
樱花落木兰35 分钟前
分布式登录实战:Session 会话共享改造,Redis 存储用户登录状态
java·javascript·数据库·redis·分布式·缓存
wuyk55538 分钟前
Python 网络爬虫入门到实战 第 04 章:请求头、UA 伪装、超时、异常处理、基础反爬绕过
开发语言·爬虫·python
HAHAXX842 分钟前
电商RPA批量上架通用方案:一套流程如何同时跑通拼多多、抖店、淘宝和跨境平台
java·运维·rpa
流形填表42 分钟前
题库去重实战:基于题干指纹的重复题清理
开发语言·c#