Java SE String类(一):常用方法(上)

news/2025/2/15 16:08:07/

1. 常用方法

1.1 字符串构造

String类的常用构造方法只有以下三种

public class Main {public static void main(String[] args) {String s1 = "hello";//使用常量串进行构造String s2 = new String("hello");//创建String对象char[] array = {'h','e','l','l','o'};//使用字符数组String s3 = new String(array);System.out.println(s1);System.out.println(s2);System.out.println(s3);}
}

如要使用其他String方法,大家可以查看源码或者是jdk帮助手册
【注意】

  1. String类是引用类型的对象内部其实并不存在字符串本身,我们可以查看String类的部分源码:
public final class Stringimplements java.io.Serializable, Comparable<String>, CharSequence,Constable, ConstantDesc {private final byte[] value;//用于存字符串的数组,字符存储在这个数组中形成了字符串private int hash; // 默认为0public String(String original) {//对以上两个成员变量进行构造this.value = original.value;this.hash = original.hash;}}

下面我们用一段代码来说明String是引用类型

public class Main {public static void main(String[] args) {String s1 = "hello";String s2 = new String("world");char[] array = {'h','i'};//创建了3个不同的引用对象String s3 = new String(array);System.out.println(s1);System.out.println(s2);System.out.println(s3);String s4 = s3;//s4和s3指向了同一个引用System.out.println(s4);}
}

字符串的引用方式其实和我们前面提到的数组很类似,因为底层就是数组实现的,我们下面通过画图说明
在这里插入图片描述
2. 在Java中,直接用双引号引起来的对象也是String类

System.out.println("hello");

1.2 字符串的常规方法

  1. 计算长度
public class Main {public static void main(String[] args) {String s1 = "hello";String s2 = new String("world");char[] array = {'h','i'};//创建了3个不同的引用对象String s3 = new String(array);System.out.println(s1);System.out.println(s2);System.out.println(s3);String s4 = s3;//s4和s3指向了同一个引用System.out.println(s4);System.out.println(s3.length());}
}

在这里插入图片描述
我们在这里需要注意的一点是,Java中的字符串不想c语言一样有以‘\0’这样的转义字符结尾的说法有几个字母,字符串就是多长,在上面的运行结果我们可以看到,s3的长度为2

  1. 判空
public class Main {public static void main(String[] args) {String s1 = "hello";String s2 = new String("");//s2为空字符char[] array = {'h','i'};//创建了3个不同的引用对象String s3 = new String(array);System.out.println(s1);System.out.println(s2);System.out.println(s3);String s4 = s3;//s4和s3指向了同一个引用System.out.println(s4);System.out.println(s3.length());System.out.println(s2.isEmpty());}
}

在这里插入图片描述
从上述结果我们可以看出,isEmpty方法如果字符串为空,返回ture,如果不为空,返回false

1.3 String对象的比较

字符串的比较也是常见的操作之一,比如:字符串的排序,Java中总共提供了4种方式

  1. == 比较是否引用同一个对象
    注意:对于内置类型,比较的是变量中的值,对于引用类型比较的是引用中的地址
public class Main {public static void main(String[] args) {int a = 1;int b = 1;int c = 2;String s1 = "hello";String s2 = "hi";String s3 = "hello";String s4 = s1;System.out.println(a == b);//两个值相等System.out.println(a == c);//两个值不相等System.out.println(s1 == s2);//两个字符串不一样System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同System.out.println(s1 == s4);//指向同一个引用}
}

在这里插入图片描述

需要注意的是,由于String是引用类型,虽然s1和s3相同,但是它们的引用不同,所以返回false
那么我们如果想要比较两个字符串是否相同,我们有办法吗,当然有,我们下面来介绍它

  1. boolean equals(Object anObject) 方法:按照字典序比较
    字典序:按照字符的大小顺序
    String重写了父类Object方法,Objec中默认使用“==”进行比较,String重写后按照如下规则比较:
public boolean equals(Object anObject) {// 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回trueif (this == anObject) {return true;}// 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回falseif (anObject instanceof String) {// 将anObject向下转型为String类型对象String anotherString = (String)anObject;int n = value.length;// 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回falseif (n == anotherString.value.length) {char v1[] = value;char v2[] = anotherString.value;int i = 0;// 4. 按照字典序,从前往后逐个字符进行比较while (n-- != 0) {if (v1[i] != v2[i])return false;i++;}return true;}}return false;
}
public class Main {public static void main(String[] args) {int a = 1;int b = 1;int c = 2;String s1 = "hello";String s2 = "hi";String s3 = "hello";String s4 = s1;System.out.println(a == b);//两个值相等System.out.println(a == c);//两个值不相等System.out.println(s1 == s2);//两个字符串不一样System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同System.out.println(s1 == s4);//指向同一个引用System.out.println(s1.equals(s3));//两个字符串相同System.out.println(s2.equals(s1));//两个字符串不同}
}

在这里插入图片描述
上述代码我们可以看到,虽然s1和s3指向的是不同的引用,但是只要字符串相同,就返回true
3. int compareTo(String anotherString) 方法:按照字典序比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:

  1. 先按照字典次序大小比较,如果出现不等的字符直接返回这两个字符的大小差值
  2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public class Main {public static void main(String[] args) {String s1 = "hello";String s2 = "hi";String s3 = "hello";String s4 = "helloaaa";System.out.println(s1 == s2);//两个字符串不一样System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同System.out.println(s1 == s4);//指向同一个引用System.out.println(s1.equals(s3));//两个字符串相同System.out.println(s2.equals(s1));//两个字符串不同System.out.println(s1.compareTo(s2));//返回字典序差值System.out.println(s1.compareTo(s3));//相同返回0System.out.println(s1.compareTo(s4));//前几个字符相同,返回字符串长度只差}}

在这里插入图片描述
4. int compareToIgnoreCase(String str) 方法:与compareTo不同的是忽略大小写进行比较

public class Main {public static void main(String[] args) {String s1 = "Hello";String s2 = "hi";String s3 = "hello";String s4 = "helloaaa";System.out.println(s1 == s2);//两个字符串不一样System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同System.out.println(s1 == s4);//指向同一个引用System.out.println(s1.equals(s3));//两个字符串相同System.out.println(s2.equals(s1));//两个字符串不同System.out.println(s1.compareTo(s2));//返回字典序差值System.out.println(s1.compareTo(s3));//相同返回0System.out.println(s1.compareTo(s4));//前几个字符相同,返回字符串长度只差System.out.println(s1.compareToIgnoreCase(s3));//忽略大小写进行比较}
}

在这里插入图片描述

1.3 字符串查找

字符串查找也是字符串中非常常见的操作,String类提供了如下查找方法:
在这里插入图片描述

public class Main {public static void main(String[] args) {String s = "aaabbbcccddd";System.out.println(s.charAt(2));System.out.println(s.indexOf('a'));System.out.println(s.indexOf('a',2));System.out.println(s.indexOf("bb"));System.out.println(s.indexOf("bb",4));System.out.println(s.lastIndexOf('d'));System.out.println(s.lastIndexOf('d',10));System.out.println(s.lastIndexOf("dd"));System.out.println(s.lastIndexOf("dd",10));}}

在这里插入图片描述

1.4 转化

  1. 数值和字符串转化
public class Main {public static void main(String[] args) {String s1 = String.valueOf(12);String s2 = String.valueOf(12.12);String s3 = String.valueOf(true);System.out.println(s1);System.out.println(s2);System.out.println(s3);int a = Integer.parseInt(s1);double b = Double.parseDouble(s2);System.out.println(s1);System.out.println(s2);}
}

在这里插入图片描述
2. 大小写转换

public class Main {public static void main(String[] args) {String s1 = "me";String s2 = "YOU";System.out.println(s1.toUpperCase());System.out.println(s2.toLowerCase());}
}

在这里插入图片描述
注意:这里在转化的是一个新的字符串,不是在原来的字符串上修改,因为字符串具有不可变性
3. 字符串转数组和字符串转数组

public class Main {public static void main(String[] args) {String s1 = "abcdefg";char[] array = s1.toCharArray();System.out.println(Arrays.toString(array));String s2 = new String(array);System.out.println(s2);}
}

在这里插入图片描述
4. 格式化字符串

public class Main {public static void main(String[] args) {String s = String.format("%d-%d-%d",2024,3,11);System.out.println(s);}
}

在这里插入图片描述


http://www.ppmy.cn/news/1380312.html

相关文章

python的数据容器--字符串

字符串的取值 my_str "my name is zhou jie lun" valuemy_str[2] value2my_str[-10]print(f"从字符串{my_str}取下标为2的元素{value} 倒数16的值是{value2}")字符串的index查找方法 valmy_str.index("is") print(f"{val}")字符串的…

MonkeyRunner在自动化测试里的应用场景!

MonkeyRunner是Android提供的一个自动化测试工具&#xff0c;主要用于对Android设备或模拟器进行功能和压力测试。以下是一些MonkeyRunner在自动化测试中的应用场景及实例代码&#xff1a; 基本操作测试 点击屏幕上的特定位置或元素。 模拟滑动和手势操作。 发送按键事件。 …

2023年第三届中国高校大数据挑战赛第二场赛题D题赛题:行业职业技术培训能力评价(成品论文 代码与思路 视频讲解)

赛题 中国是制造业大国&#xff0c;产业门类齐全&#xff0c;每年需要培养大量的技能娴熟的技术工人进入工厂。某行业在全国有多所不同类型&#xff08;如国家级、省级等&#xff09;的职业技术培训学校&#xff0c;进行 5 种技能培训。学员入校时需要进行统一的技能考核&…

C语言函数—库函数

函数是什么&#xff1f; 数学中我们常见到函数的概念。但是你了解C语言中的函数吗&#xff1f; 维基百科中对函数的定义&#xff1a;子程序 在计算机科学中&#xff0c;子程序&#xff08;英语&#xff1a;Subroutine, procedure, function, routine, method, subprogram, ca…

八股文-持续更新......

文章目录 SpringSpringBootDubboMQMysqlNettyRedis并发ZookeeperMybatisElasticsearchLinux微服务ZookeeperMybatisElasticsearchLinux

day1-C++

1>提示并输入一个字符串&#xff0c;统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数要求使用C风格字符串完成。 代码&#xff1a; #include <iostream> #include <string.h> using namespace std;int main() {string str ;int low 0, …

语音情感基座模型emotion2vec

在语音技术领域&#xff0c;准确理解用户的语音指令和意图是构建高效人机交互系统的基础。一个高品质的语音交互系统不仅需要理解字面上的语言内容&#xff0c;更应捕捉到说话者语音中蕴含的情感信息。这正是语音情感识别&#xff08;SER&#xff09;技术要解决的问题&#xff…

Python与FPGA——图像锐化

文章目录 前言一、图像锐化二、Python robert锐化三、Python sobel锐化四、Python laplacian锐化五、FPGA sobel锐化总结 前言 在增强图像之前一般会先对图像进行平滑处理以减少或消除噪声&#xff0c;图像的能量主要集中在低频部分&#xff0c;而噪声和图像边缘信息的能量主要…