文章目录
String类的介绍
什么是字符串
String类源码是这样的:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
/**
* Class String is special cased within the Serialization Stream Protocol.
*
* A String instance is written into an ObjectOutputStream according to
* <a href="{@docRoot}/../platform/serialization/spec/output.html">
* Object Serialization Specification, Section 6.2, "Stream Elements"</a>
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
可以看出来,Java中String类就是对将个字符转换成一个字符数组,对其进行封装,可以被运用直接形成字符串。所以字符串就是由多个字符组成的字符序列,我们可以将字符串看做一个元素为多个字符的字符数组。
字符串的特点
-
字符串属于常量,一单被创建就无法改变
看String类的源码可以看到,其中的成员变量 char value[ ] 字符数组用final修饰,所以意味着一旦这个值被初始化将不能改变,那么String的对象当然也是不可变的了。 -
字符串属于引用数据类型,其默认值为null
既然字符串为引用数据类型,所以我们可以引用String类创建对象来定义一个字符创,但是在平常的开发过程中我们并不需要通过创建对象来定义一个字符串,我们可以直接通过String 字符串名 ="字符串"
的格式来定义一个字符串。原因是在JVM中的方法区,存在一个字符串常量池,我们赋值后的字符串会直接储存在里面,当我们创建一个字符串,系统会在字符串常量池中寻找是否存储过此字符串并将其赋值给新的字符串。
比如现在我们定义两个相同的字符串,看看其引用是否相同。
结果为"true",我们并不需要通过创建新的对象来定义字符串。
String类的构造方法
public String()
:空构造
public String(byte[] bytes)
:把字节数组转成字符串
public String(byte[] bytes,int index,int length)
:把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value)
:把字符数组转成字符串public String(char[] value,int index,int count)
:把字符数组的一部分转成字符串
public String(String original)
:把字符串常量值转成字符串
String类的常用方法
String类的判断功能
方法 | 作用 |
---|---|
public boolean equals(Object obj) | 比较字符串的内容是否相同,区分大小写 |
public boolean equalsIgnoreCase(String str) | 比较字符串的内容是否相同,忽略大小写 |
public boolean contains(String str) | 判断字符串中是否包含传递进来的字符串 |
public boolean startsWith(String str) | 判断字符串是否以传递进来的字符串开头 |
public boolean endsWith(String str) | 判断字符串是否以传递进来的字符串结尾 |
public boolean isEmpty() | 判断字符串的内容是否为空串 |
String类的获取功能
方法 | 作用 |
---|---|
public int length() | 获取字符串的长度 |
public char charAt(int index) | 获取指定索引位置的字符 |
public int indexOf(int ch) | 返回指定字符在此字符串中第一次出现处的索引 |
public int indexOf(String str) | 返回指定字符串在此字符串中第一次出现处的索引 |
public int indexOf(int ch,int fromIndex) | 返回指定字符在字符串中指定位置后第一次出现处的索引 |
public int indexOf(String str,int fromIndex) | 返回指定字符串在此字符串中从指定位置后第一次出现处的索引 |
public String substring(int start) | 从指定位置开始截取字符串,默认到末尾 |
public String substring(int start,int end) | 从指定位置开始到指定位置结束截取字符串 |
String类的转换功能
方法 | 作用 |
---|---|
public byte[] getBytes() | 把字符串转换为字节数组 |
public char[] toCharArray() | 把字符串转换为字符数组 |
public static String valueOf(char[] chs) | 把字符数组转成字符串 |
public static String valueOf(int i ) | 把int类型的数据转成字符串 |
ublic String toLowerCase() | 把字符串转成小写 |
public String toUpperCase() | 把字符串转成大写 |
public String concat(String str) | 把字符串拼接 |
- 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
String类的其他功能
- String的替换功能
方法 | 作用 |
---|---|
public String replace(char old,char new) | 将指定字符进行互换 |
public String replace(String old,String new) | 将指定字符串进行互换 |
- String去除字符串两空格
方法 | 作用 |
---|---|
public String trim() | 去除两端空格 |