String对象中方法总结

String对象中方法总结

String对象中实际储存在value字符数组中

/** The value is used for character storage. */
/** 该值用于字符存储。 */
private final char value[];

同时还存有一个hash

/** Cache the hash code for the string */
/** 缓存字符串的哈希码 */
private int hash; // Default to 0

同时String还提供了非常丰富的构造方法,可以通过字符串,char[],Unicode,ascii[],bytes[]来创建String对象。

函数:

length()

返回String 对象中的value的length

上边提到了value是一个char[]因此他有length属性

isEmpty()

用于判断对象是否存有字符串内容。

依然使用的value的length属性进行判断

public boolean isEmpty() {
    return value.length == 0;
}

charAt

获取第N个字符

同样适用的是数组的方式获取

public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}

codePointAt

返回指定的字符(Unicode 值)

不太常用,比如

String str = "a";
System.out.println(a.codePointAt(0));

其返回结果为51

codePointBefore

他返回的是制定位置前一个字符的Unicode 值

String str = "ab";
System.out.println(a.codePointBefore(1));

返回结果51

codePointCount

public int codePointCount(int beginIndex, int endIndex)

通过源码发现他是返回给定范围unicode个数,也就是endIndex-beginIndex的值

不知道这个可以具体什么场景用有待研究

offsetByCodePoints

public int offsetByCodePoints(int index, int codePointOffset) 

通过源码可见他是获取从index开始偏移到codePointOffset时是在字符串的第几个位置

getChars

获取从一个字符串开始位置到结束为止所有字符并将它们放入字符数组

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)

dst是需要准备的数组,截取后的字符会根据dstBegin为数组开始位置一次放入dst数组中

getBytes

public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) 

不用多说了获取byte数组的方法,具体实现跟getChars基本相同。不过该方法已弃用

getBytes

public byte[] getBytes(String charsetName)

该方法替代上一个getBytes,是比较实用的方法。charsetName需要指定的编码规则如:“UTF-8”。

public byte[] getBytes(Charset charset)

这是他的另一个重载方法他传入的编码规则需要使用Charset,例如:StandardCharsets.UTF_8

public byte[] getBytes()

最为常用的获取byte数组的方式。

equals

这个都比较了解了判断两个字符串内容是否想动而不是对象。当然如果对象本身相同返回的肯定是true

不同的String首先会对比char[]的长度,长度相同时会一位一位的对char[]中的内容。

contentEquals

public boolean contentEquals(CharSequence cs)

这个方法跟equals的区别就在于比较的类型更加丰富,常用的String、StringBuffer、StringBuilder都实现了接口java.lang.CharSequence。

还有个

public boolean contentEquals(StringBuffer sb)

感觉这个方法就没什么必要了只是在里边把StringBuffer强转了一下java.lang.CharSequence

equalsIgnoreCase

很实用的方法,它主要是用来忽略大小写进行比较。

compareTo

很少使用这个方法,他也是按照字典顺序来比较字符串只是结果返回的-1,0,1

Comparator

一个可以序列化的比较器

compareToIgnoreCase

同样忽略大小写的比较器

regionMatches

public boolean regionMatches(int toffset, String other, int ooffset,
        int len) 

用来比较字符串中的两个区间内的字符是否相同

public boolean regionMatches(boolean ignoreCase, int toffset,
        String other, int ooffset, int len) {

作用同上,只是多了一个ignoreCase参数如果为true则忽略大消息

startsWith

非常实用的方法

public boolean startsWith(String prefix, int toffset)

用来判断字符串是否已某个字符串为开头,toffset偏移量用来制定开头的位置,可以从0也可以指定。

public boolean startsWith(String prefix)

默认没有偏移量也就是从0开始

endsWith

public boolean endsWith(String suffix)

顾名思义判断是否已某个字符串为结尾,其内部其实是调用的startsWith,通过长度设置了偏移量。

hashCode

获取字符串的hash值

indexOf

public int indexOf(int ch)

获取传入的unicode在字符串中第一次出现的位置

public int indexOf(int ch, int fromIndex)

同上只是增加了偏移量,上一个方法内部调用的是这个方法偏移量0

public int indexOf(String str)

获取字符串在本对象中第一次出现的位置,最常用的方法

public int indexOf(String str, int fromIndex)

不再过多赘述。。。。

lastIndexOf

获取传入的unicode在字符串中最后一次出现的位置

public int lastIndexOf(int ch) 
public int lastIndexOf(int ch, int fromIndex)
public int lastIndexOf(String str)

入参字符串其他作用相同

public int lastIndexOf(String str, int fromIndex)

substring

重点来啦

public String substring(int beginIndex)

从第几个开始截取从1开始计算

String str = "ab@cda";
System.out.println(str.substring(1));

输出结果:b@cda

public String substring(int beginIndex, int endIndex)

直接上代码看疗效

String str = "ab@cda";
System.out.println(str.substring(1,3));

输出结果:b@

subSequence

String str = "ab@cda";
System.out.println(str.substring(1,5));
System.out.println(str.subSequence(1,5));

输出:b@cd
b@cd

所以这俩目前没看出区别吧,其实呢subSequence调用的就是substring。。。完全没有任何自己的处理

concat

比较优雅的拼接字符串的方法用来代替+号他是从内部数组着手的因此他不会生成新的String对象,很推荐这个方法。不创建多余的对象爽不爽~

replace

替换,简单实用

public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)

入参不同作用相同

replaceFirst

支替换第一个出现的字符串,可使用正则表达式

replaceAll

public String replaceAll(String regex, String replacement) 

通过正则表达式替换所有字符

matches

public boolean matches(String regex)

根据正则表达式进行判断。

contains

public boolean contains(CharSequence s)

判断字符串是否包含某个字符或字符串。

split

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

根据某个字符或正则转换数组

join

很多重构,总体上就是将数组转换成以某个特定字符分隔的字符串或者说是将 数组每一个元素中间加入特定字符并转换成字符串

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
String join = String.join(",", list);
System.out.println(join);

输出结果:a,b,c

toLowerCase

转换为小写

toUpperCase

转换为大写

trim

去前后空格不多说

toString

强转字符串

toCharArray

将字符串转换为char[]

返回的其实就是对象内的value,只是多加了一步System.arraycopy,把内容复制到了一个新的数组中,这样对新的数组进行修改不会影响这个对象内的value

format

返回使用指定语言环境、格式字符串和参数的格式化字符串。

public static String format(Locale l, String format, Object... args)

valueOf

对象转换为字符串

public static String valueOf(Object obj)
public static String valueOf(char data[])
public static String valueOf(char data[], int offset, int count)
public static String copyValueOf(char data[], int offset, int count)

作用跟valueOf(char data[], int offset, int count)相同

public static String copyValueOf(char data[])

作用跟valueOf(char[])相同

public static String valueOf(boolean b) {
        return b ? "true" : "false";
    }

这个一目了然。。

public static String valueOf(char c)

char转String还有很多不一一赘述

intern

最后一个

public native String intern();

native方法因此看不到源码,注释里边的意思是返回字符串对象的规范表示。

String str = "a/b@c\nda";
System.out.println(str.intern());

输出结果:

a/b@c
da

感觉恩。。没什么感觉

以上就是String对象内部的绝大部分方法,有一些重载由于大同小异没有完全列举。
本文仅供自己学习总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值