栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022.0423学习Java笔记之String类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022.0423学习Java笔记之String类

基本概念

String类是一个较为特殊的类,而这个类的对象可以使用两种模式进行实例化

  • 直接赋值        例如:String  str = "Hello"
  • String本身是一个类,是类就会有构造方法,String类提供了一个构造方法:public String(String str)

字符串比较

在引用数据类型操作之中,使用“==”比较的是地址数值,但是比较也有用的内容,所以String提供一个比较方法:public boolean equals(String str)

String类两种实例化区别

String类不同直接赋值会保存在一个字符串池里面,而使用构造方法实例化String类是会开辟两块空间,其中有一块是垃圾空间,但在String类里面提供了入池方法:public String intren()

字符串一旦声明,则内容不可改变

字符串的改变依靠的是“地址引用关系”的改变而改变,这样不仅性能差,还会产生大量垃圾空间

 

String类常用方法

其中1~4是字符串与字符有关的,5~8是字符串与字节有关,9~11是字符串比较有关,12~19是字符串查找有关,20~21是字符串替换有关的,22~23是字符串拆分有关,24~25是字符串截取有关,26~32是其他方法

No

方法名称

类型

描述

1

Public String(char[] value)

构造

将指定字符数组变成字符串

2

public String(char[] value, int offset, int count)

构造

将指定范围字符数组变成字符串

3

public char charAt(int index)

普通

取得字符串中指定索引位置的字符

4

public char[] toCharArray()

普通

将字符串变为字符数组

5

public String(byte[] bytes)

构造

将字节数组变为字符串

6

public String(byte[] bytes, int offset, int length)

构造

将指定范围字节数组变为字符串

7

public byte[] getBytes()

普通

将字符串变为字节数组

8

public byte[] getBytes(String charsetName)throws UnsupportedEncodingExceptione

普通

编码转换

9

public boolean equals(String anObject)

普通

判断两个字符串是否相等,区分大小写

10

public boolean equalsIgnoreCase(String anotherString)

普通

判断两个字符串是否相等,不区分大小写

11

public int compareTo(String anotherString)

普通

比较两个字符串的大小关系

12

public boolean contains(String s)

普通

判断字符串是否存在

13

public int indexOf(String str)

普通

从头查找字符串的指定位置,如果找到返回位置索引,找不到返回-1

14

public int indexOf(String str, int fromIndex)

普通

从指定位置由前向后查

15

public int lastIndexOf(String str)

普通

从后向前查找指定字符串位置

16

public int lastIndexOf(String str, int fromIndex)

普通

从指定位置由后向前查找字符串位置

17

public boolean startsWith(String prefix)

普通

判断是否以指定的字符串开头

18

public boolean startsWith(String prefix, int toffset)

普通

从指定位置判断是否以指定字符串开头

19

public boolean endsWith(String suffix)

普通

判断是否以指定字符串结尾

20

public String replace All(String regex, String replacement)

普通

全替换

21

public String replaceFirst(String regex, String replacement)

普通

替换首个

22

public String[] split(String regex)

普通

全拆分

23

public String[] split(String regex, int limit)

普通

部分拆分

24

public String substring(int beginIndex)

普通

由开始截取到结尾

25

public String substring(int beginIndex, int endIndex)

普通

设置开始和结束索引进行截取

26

public String concat(String str)

普通

字符串连接

27

public String intern()

普通

如池

28

public boolean isEmpty()

普通

判断是否是空字符串

29

public int length()

普通

取得字符串长度

30

public String toIowerCase()

普通

转小写

31

public String toUpperCase()

普通

转大写

32

public String trim()

普通

去掉左右空格

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号