Java中String类简介

概述

String类表示字符串,Java程序中的所有字符串文字(例如"abc" )都实现为此类的实例。
字符串(String)是不变的; 它们的值在创建后无法更改。 字符串缓冲区(StringBuffer和StringBuilder)支持可变字符串。之所以会产生这样的区别是因为String类储存在方法区中(现在叫元空间),字符串缓冲区储存在堆中。
需要注意的是:我们在使用String类时,每一个操作都会产生一个新的String类,尤其是在使用多个"+"号多个字符串相连时,会产生很多String类的中间值,这些值虽然没有引用,但由于String类储存在元空间中,这些中间值不会被回收,所以在大量使用字符串连接符时用字符串缓冲区会更节省内存。
另外,API中还有这样一句话:因为String对象是不可变的,所以可以共享它们。这句话是说如果两个字符串的值一样,就视为同一个字符串,观察下列代码:

    public static void main(String[] args) {
        String str1 = "abcd";
        String str2 = "abcd";
        System.out.println(str1 == str2);
    }

运行结果是:true,这里的“==”比较的并不是str1和str2的值,而是两者的地址,说明这两者的地址一样,这是因为在创建str2的时候会自动的在内存中去找有没有值为“abcd”的字符串,内存中有“abcd”字符串,与是就直接引用str1的地址,不再创建一个对象。
如果要新创建一个字符串,就要采用new的方法:

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

这里运行结果为:false,说明两者的地址不一样了

String的构造方法

构造器描述
String()初始化新创建的 String对象,使其表示空字符序列。
String​(byte[] bytes)通过使用平台的默认字符集解码指定的字节数组构造新的 String 。
String​(byte[] ascii, int hibyte)已过时。 此方法无法将字节正确转换为字符。
String​(byte[] bytes, int offset, int length)通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
String​(byte[] ascii, int hibyte, int offset, int count)已过时。 此方法无法将字节正确转换为字符。
String​(byte[] bytes, int offset, int length, String charsetName)通过使用指定的字符集解码指定的字节子 String构造新的 String 。
String​(byte[] bytes, int offset, int length, Charset charset)通过使用指定的charset解码指定的字节子String构造新的String 。
String​(byte[] bytes, String charsetName)构造一个新的String由指定用指定的字节的数组解码charset 。
String​(byte[] bytes, Charset charset)构造一个新的String由指定用指定的字节的数组解码charset 。
String​(char[] value)分配新的 String ,使其表示当前包含在字符数组参数中的字符序列。
String​(char[] value, int offset, int count)分配一个新的 String ,其中包含字符数组参数的子数组中的字符。
String​(int[] codePoints, int offset, int count)分配新的 String ,其中包含 Unicode code point数组参数的子数组中的字符。
String​(String original)初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。
String​(StringBuffer buffer)分配一个新字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。
String​(StringBuilder builder)分配一个新字符串,其中包含当前包含在字符串构建器参数中的字符序列。

String类的常用方法

变量和类型方法描述
charcharAt​(int index)返回指定索引处的 char值。
intcodePointAt​(int index)返回指定索引处的字符(Unicode代码点)。
intcodePointBefore​(int index)返回指定索引之前的字符(Unicode代码点)。
intcodePointCount​(int beginIndex, int endIndex)返回此 String的指定文本范围内的Unicode代码点数。
intcompareTo​(String anotherString)按字典顺序比较两个字符串。
intcompareToIgnoreCase​(String str)按字典顺序比较两个字符串,忽略大小写差异。
Stringconcat​(String str)将指定的字符串连接到此字符串的末尾。
booleancontains​(CharSequence s)当且仅当此字符串包含指定的char值序列时,才返回true。
static StringcopyValueOf​(char[] data)相当于 valueOf(char[]) 。
static StringcopyValueOf​(char[] data, int offset, int count)相当于 valueOf(char[], int, int) 。
booleanendsWith​(String suffix)测试此字符串是否以指定的后缀结尾。
booleanequals​(Object anObject)将此字符串与指定的对象进行比较。
booleanequalsIgnoreCase​(String anotherString)将此 String与另一个 String比较,忽略了大小
byte[]getBytes()使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
inthashCode()返回此字符串的哈希码。
intindexOf​(int ch)返回指定字符第一次出现的字符串中的索引。
intindexOf​(int ch, int fromIndex)返回指定字符第一次出现的此字符串中的索引,从指定索引处开始搜索。
intindexOf​(String str)返回指定子字符串第一次出现的字符串中的索引。
intindexOf​(String str, int fromIndex)从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。
Stringintern()返回字符串对象的规范表示。
intlastIndexOf​(int ch)返回指定字符最后一次出现的字符串中的索引。
intlastIndexOf​(int ch, int fromIndex)返回指定字符最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
intlastIndexOf​(String str)返回指定子字符串最后一次出现的字符串中的索引。
intlastIndexOf​(String str, int fromIndex)返回指定子字符串最后一次出现的字符串中的索引,从指定索引开始向后搜索。
intlength()返回此字符串的长度。
Stringrepeat​(int count)返回一个字符串,其值为此字符串的串联重复 count次。
Stringstrip()返回一个字符串,其值为此字符串,并删除了所有前导和尾随 white space 。
StringstripLeading()返回一个字符串,其值为此字符串,并删除了所有前导 white space 。
StringstripTrailing()返回一个字符串,其值为此字符串,并删除所有尾随 white space 。
char[]toCharArray()将此字符串转换为新的字符数组。
static StringvalueOf​(boolean b)返回 boolean参数的字符串表示形式。
static StringvalueOf​(char c)返回 char参数的字符串表示形式。
static StringvalueOf​(char[] data)返回 char数组参数的字符串表示形式。
static StringvalueOf​(char[] data, int offset, int count)返回 char数组参数的特定子数组的字符串表示形式。
static StringvalueOf​(double d)返回 double参数的字符串表示形式。
static StringvalueOf​(float f)返回 float参数的字符串表示形式。
static StringvalueOf​(int i)返回 int参数的字符串表示形式。
static StringvalueOf​(long l)返回 long参数的字符串表示形式。
static StringvalueOf​(Object obj)返回 Object参数的字符串表示形式。

String、StringBuffer和StringBuilder的区别

  1. String是不可变类,StringBuffer和StringBuilder是可变类。
  2. StringBuffer是线程安全的,因为它相关方法都加了synchronized 关键字,StringBuilder线程不安全。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值