当前位置: 首页> 游戏> 游戏 > java中的String 以及其方法(超详细!!!)

java中的String 以及其方法(超详细!!!)

时间:2025/7/11 8:46:37来源:https://blog.csdn.net/qq_55846232/article/details/140400610 浏览次数:0次

文章目录

  • 一、String类型是什么
    • String不可变的原因(经典面试题)
    • String不可变的好处
  • 二、String的常用构造形式
    • 1.使用常量串构造
    • 2.使用newString对象构造
    • 3.字符串数组构造
  • 三、常用方法
    • 1. length() 获取字符串的长度
    • 2. charAt() 获取字符串中指定字符的值 (代码单元)
    • 3. codePointAt() 获取字符串中指定字符的值的代码点(编码值) (Ascll码值)
    • 4.equals() 比较字符串是否相等
      • 【补充】(扩展) == 与 equals的区别
    • 5.substring(a,b) 字符串截取 要a不要b 要头不要尾
    • 6.indexOf() 查找某个子字符串所在的位置 ,如果存在 返回第一次出现子串的具体位置的索引值,如果不存在 返回-1
    • 7. lastIndexOf() 查找某个子字符串所在最后一次的位置,如果存在 返回最后一次出现子串的具体位置的索引值,如果不存在 返回-1
    • 8.startsWith() 是否是以某个字串开头的 ,如果是 返回true 如果不是 返回false
    • 9.endsWith() 是否是以某个字串结尾的 , 如果 是 返回true 如果 不是 返回false
    • 10.contains() 是否包含某个字串 , 如果 是 返回true 如果 不是 返回false
    • 11.replace(olderchar,newchar)替换某个子字符串 replaceAll()替换所有
      • 【扩展】[replace 与 replaceAll 的区别](https://blog.csdn.net/qq_55846232/article/details/140401676?spm=1001.2014.3001.5502)
    • 12.toUpperCase() 把字母转换成大写
    • 13.toLowerCase() 把字母转换成小写
    • 14.trim() 去字符串开头结尾空格

一、String类型是什么

String类型其实就是定义字符串的一种类型,它可以直接定义我们想要表示的字符串
String是不可变类型

String不可变的原因(经典面试题)

在这里插入图片描述

1.String是final类型的类,value也是final类型的数组,但这不是String不可变的根本原因,String不可变是因为value是private,且并没有提供对外的get和set
2.底层char[]数组有final修饰,意味着这个数组不能扩容等,来达到存更多的字符,final修饰char类型数组,保证数组一旦被赋值 不能在重新赋值,修改char类型数组后,地址不能改变,内容可变,但是没有具体的方法去修改内容
3.char[]数组是私有的,我们程序员无法直接操作这个char[]数组,而且String没有提供这样的方法,来修改char[]数组的元素的值。

String不可变的好处

可以共享,也是为了安全

二、String的常用构造形式

1.使用常量串构造

public static void main(String[] args) {String s1 = "asd";System.out.println(s1);}

运行结果:

asd

2.使用newString对象构造

 public static void main(String[] args) {String s1 = new String("asdasd");System.out.println(s1);}

运行结果:

asdasd

3.字符串数组构造

public static void main(String[] args) {char[] array = {'h','e','l','l','o'};String s1 = new String(array);System.out.println(s1);}

运行结果:

hello

需要注意的是字符串数组里的数据要用单引号不要使用双引号

三、常用方法

1. length() 获取字符串的长度

String test = "asdasdasdasd";
System.out.println(test.length());

运行结果

12

2. charAt() 获取字符串中指定字符的值 (代码单元)

String test = "asdasdasdasd";
System.out.println(test.charAt(0));

运行结果

a

3. codePointAt() 获取字符串中指定字符的值的代码点(编码值) (Ascll码值)

String test = "asdasdasdasd";
System.out.println(test.codePointAt(0));

运行结果

97

4.equals() 比较字符串是否相等

String test = "asdasdasdasd";
String test1 = "aaaadddd";
System.out.println(test.equals(test1));

运行结果

false

【补充】(扩展) == 与 equals的区别

String str3 = new String(“123”) 在内存中传递过程 会开辟两个或三个空间
String str1 = “123” 与String str3 = new String(“123”)不相同 是因为地址不同
== 在java中的含义是比较地址
首先 ,equals方法是不能作用与基本数据类型的变量
如果没有对equals 方法进行重写,则它比较的是引用类型变量的地址
像String、Data等类对equals方法进行了重写的话,比较的是所指向的对象的内容

public static void main(String[] args) {String test = "asdasdasdasd";String test1 = new String("asdasdasdasd");System.out.println(test == test1);System.out.println(test.equals(test1));}

运行结果

false
true

5.substring(a,b) 字符串截取 要a不要b 要头不要尾

String s1 = "asdasdasdasd";
System.out.println(s1.substring(0,4));

运行结果

asda

6.indexOf() 查找某个子字符串所在的位置 ,如果存在 返回第一次出现子串的具体位置的索引值,如果不存在 返回-1

String s1 = "asdasdasdasd";
System.out.println(s1.indexOf("a"));
System.out.println(s1.indexOf("a",1));  //1的意思是跳过一个字符  就是跳过a字符 在寻找

运行结果

0
3

7. lastIndexOf() 查找某个子字符串所在最后一次的位置,如果存在 返回最后一次出现子串的具体位置的索引值,如果不存在 返回-1

String test = "asdasdasdasd"
System.out.println(test.lastIndexOf("d"));

运行结果

11

8.startsWith() 是否是以某个字串开头的 ,如果是 返回true 如果不是 返回false

String s1 = "asdasdasdasd";
System.out.println(s1.startsWith("as"));
System.out.println(s1.startsWith("sd"));

运行结果

true
false

9.endsWith() 是否是以某个字串结尾的 , 如果 是 返回true 如果 不是 返回false

String s1 = "asdasdasdasd";
System.out.println(s1.endsWith("as"));
System.out.println(s1.endsWith("sd"));

运行结果

false
true

10.contains() 是否包含某个字串 , 如果 是 返回true 如果 不是 返回false

 String s1 = "asdasdasdasd";
System.out.println(s1.contains("as"));

运行结果

true

11.replace(olderchar,newchar)替换某个子字符串 replaceAll()替换所有

String s1 = "asdasdasdasd,\r";
System.out.println(s1.replace("as","11"));
System.out.println(s1.replaceAll("\r","*"));

运行结果

11d11d11d11d,
asdasdasdasd,*

【扩展】replace 与 replaceAll 的区别

12.toUpperCase() 把字母转换成大写

String s1 = "asdasdasdasd";
System.out.println(s1.toUpperCase());

运行结果:

ASDASDASDASD

13.toLowerCase() 把字母转换成小写

String s1 = "aSdAsDasdasd";
System.out.println(s1.toLowerCase());

运行结果

asdasdasdasd

14.trim() 去字符串开头结尾空格

String s1 = " aSdAsDasdasds";
System.out.println(s1);
System.out.println(s1.trim());

运行结果
在这里插入图片描述

感谢大家的收看和支持 ,欢迎大家来指正!!!

关键字:java中的String 以及其方法(超详细!!!)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: