java.lang.String常用方法

Unicode

+属性

public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
比较String对象的比较器compareToIgnoreCase

+构造函数

public String()
初始化新创建的String对象,使其表示空字符序列

public String(String original)
始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本

public String(char[] value)
分配一个新的,String以便它表示当前包含在字符数组参数中的字符序列

public String(char[] value, int offset, int count)
分配String包含字符数组参数的子数组中的字符的new

public String(int[] codePoints, int offset, int count)
分配String包含Unicode代码点数组参数的子数组中的字符的new

public String(byte[] ascii, int hibyte, int offset, int count)
已过时。
此方法无法将字节正确转换为字符。从JDK 1.1开始,执行此操作的首选方法是使用String带有Charsetcharset名称或使用平台默认字符集的 构造函数

public String(byte[] ascii, int hibyte)
已过时。
此方法无法将字节正确转换为字符。从JDK 1.1开始,执行此操作的首选方法是使用String带有Charsetcharset名称或使用平台默认字符集的 构造函数

public String(byte[] bytes, int offset, int length, String charsetName)
String通过使用指定的字符集解码指定的字节子数组构造一个new

public String(byte[] bytes, int offset, int length, Charset charset)
String通过使用指定的字符集解码指定的字节子数组构造一个new

public String(byte[] bytes, String charsetName)
String通过使用指定的字符集解码指定的字节数组构造一个new

public String(byte[] bytes, Charset charset)
String通过使用指定的字符集解码指定的字节数组构造一个new

public String(byte[] bytes, int offset, int length)
String通过使用平台的默认字符集解码指定的字节子数组构造一个new

public String(byte[] bytes)
String通过使用平台的默认字符集解码指定的字节数组来构造new

public String(StringBuffer buffer)
分配一个新字符串,其中包含当前包含在字符串缓冲区参数中的字符序列

public String(StringBuilder builder)
分配一个新字符串,其中包含当前包含在字符串构建器参数中的字符序列

+公共方法

public int length()
返回此字符串的长度

public boolean isEmpty()
判断此字符串是否为空

public char charAt(int index)
返回char指定索引处的值。索引范围从。0到 length() - 1。char序列的第一个值是索引0,下一个是索引1,依此类推,就像数组索引一样

public int codePointAt(int index)
返回指定索引处的字符(Unicode代码点)

public int codePointBefore(int index)
返回指定索引之前的字符(Unicode代码点)

public int codePointCount(int beginIndex, int endIndex)
返回指定文本范围内的Unicode代码点数String

    String strs = "\uD835\uDD6B\uD835\uDD6B";
    System.out.println(strs);		// 结果:??
    System.out.println(strs.length()); // 结果:4
    System.out.println(strs.codePointCount(0,strs.length())); // 结果:2

public int offsetByCodePoints(int index, int codePointOffset)
返回此序列中的索引,该索引是从给定 index 偏移 codepointoffset 个代码点后得到的

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
将此字符串中的字符复制到目标字符数组中

public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)
已过时
此方法无法将字符正确转换为字节。从JDK 1.1开始,首选方法是通过 getBytes()使用平台默认字符集的方法

public byte[] getBytes(String charsetName)
String使用命名的字符集将其编码为字节序列,将结果存储到新的字节数组中

public byte[] getBytes(Charset charset)
String使用给定的字符集将其编码为字节序列 ,将结果存储到新的字节数组中

public byte[] getBytes()
String使用平台的默认字符集将其编码为字节序列,将结果存储到新的字节数组中

public boolean equals(Object anObject)
将此字符串与指定的对象进行比较

public boolean contentEquals(StringBuffer sb)
将此字符串与指定字符串进行比较StringBuffer

public boolean contentEquals(CharSequence cs)
将此字符串与指定字符串进行比较CharSequence

public boolean equalsIgnoreCase(String anotherString)
将此String与另一个进行比较String,忽略大小写

public int compareTo(String anotherString)
按字典顺序比较两个字符串

public int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,忽略大小写差异

public boolean regionMatches(int toffset, String other, int ooffset, int len)
当某个字符串调用该方法时,表示从当前字符串的firstStart位置开始,取一个长度为len的子串;
然后从另一个字符串other的otherStart位置开始也取一个长度为len的子串,然后比较这两个子串是
否相同,如果这两个子串相同则返回true,否则返回false

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
与public boolean regionMatches(int toffset, String other, int ooffset, int len)大致相同,其中如果ignoreCase等于true则忽略大小写,否则不忽略

public boolean startsWith(String prefix, int toffset)
从指定索引处开始测试此字符串是否以指定的前缀开头

public boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开头

public boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结尾

public int hashCode()
返回此字符串的哈希码

public int indexOf(int ch)
返回指定字符第一次出现的字符串中的索引
ch是字符串的Unicode代码点,如:a的Unicode代码点为97

public int indexOf(int ch, int fromIndex)
返回指定字符第一次出现的此字符串中的索引,从指定索引处开始搜索
ch是字符串的Unicode代码点,如:a的Unicode代码点为97

public int indexOf(String str)
返回指定字符最后一次出现的字符串中的索引

public int indexOf(String str, int fromIndex)
从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引

public int lastIndexOf(int ch)
返回指定字符最后一次出现的字符串中的索引
ch是字符串的Unicode代码点,如:a的Unicode代码点为97

public int lastIndexOf(int ch, int fromIndex)
返回指定字符最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
ch是字符串的Unicode代码点,如:a的Unicode代码点为97

public int lastIndexOf(String str)
返回指定子字符串最后一次出现的字符串中的索引
空字符串“”的最后一次出现被认为是在索引值this.length()处发生的

public int lastIndexOf(String str, int fromIndex)
返回指定子字符串最后一次出现的字符串中的索引,从指定索引开始向后搜索

public String substring(int beginIndex)
返回从beginIndex(包含beginIndex)处到末尾之间的字符串

public String substring(int beginIndex, int endIndex)
返回从beginIndex(包含beginIndex)处开始,endIndex结束之间的字符串

public CharSequence subSequence(int beginIndex, int endIndex)
返回从beginIndex(包含beginIndex)处开始,endIndex结束之间的字符序列

public String concat(String str)
将指定的字符串连接到此字符串的末尾

public boolean matches(String regex)
判断此字符串是否与给定的正则表达式匹配

public boolean contains(CharSequence s)
当且仅当此字符串包含指定的char值序列时,才返回true

public String replace(char oldChar, char newChar)
返回用newChar字符替换此字符串中所有出现的字符oldChar

public String replace(CharSequence target, CharSequence replacement)
返回用replacement字符串替换此字符串中所有出现的字符target

public String replaceFirst(String regex, String replacement)
如果匹配成功,使用给定的参数 replacement 替换第一次匹配成功的字符串

public String replaceAll(String regex, String replacement)
如果匹配成功,使用给定的参数 replacement 替换所有匹配成功的字符串

public String[] split(String regex, int limit)
将此字符串拆分为给定正则表达式的匹配项

public String[] split(String regex)
将此字符串拆分为给定正则表达式的匹配项

public String toLowerCase()
String使用默认语言环境的规则将此中的所有字符转换为小写

public String toLowerCase(Locale locale)
String使用给定的规则将此中的所有字符转换为小写Locale

public String toUpperCase()
String使用默认语言环境的规则将此中的所有字符转换为大写

public String toUpperCase(Locale locale)
String使用给定的规则将此中的所有字符转换为大写Locale

public String trim()
返回一个字符串,其值为此字符串,并删除任何前导和尾随空格

public String toString()
返回字符串本身

public char[] toCharArray()
将此字符串转换为新的字符数组

public native String intern()
用来返回常量池中的某字符串,如果常量池中已经存在该字符串,则直接返回常量池中该对象的引用。否则,在常量池中加入该对象,然后返回引用

+静态方法

public static String join(CharSequence delimiter, CharSequence... elements)
用于把数组中的所有元素放入一个字符串,元素是通过指定的分隔符进行分隔的
System.out.println(String.join("", “J”,“a”,“v”,“a”)); // 结果:Java

public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
用于把集合中的所有元素放入一个字符串,元素是通过指定的分隔符进行分隔的

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

public static String format(String format, Object... args)
使用指定的格式字符串和参数返回格式化字符串

public static String format(Locale l, String format, Object... args)
使用指定的语言环境,格式字符串和参数返回格式化的字符串

public static String valueOf(Object obj)
返回Object参数的字符串表示形式

public static String valueOf(char data[])
返回char数组参数的字符串表示形式

public static String valueOf(char data[], int offset, int count)
返回char数组参数的特定子数组的字符串表示形式,从char数组offset处开始,返回count个

public static String valueOf(boolean b)
返回boolean参数的字符串表示形式

public static String valueOf(char c)
返回char参数的字符串表示形式

public static String valueOf(int i)
返回int参数的字符串表示形式

public static String valueOf(long l)
返回long参数的字符串表示形式

public static String valueOf(float f)
返回float参数的字符串表示形式

public static String valueOf(double d)
返回double参数的字符串表示形式

public static String copyValueOf(char data[], int offset, int count)
相当于valueOf(char[], int, int)

public static String copyValueOf(char data[])
相当于valueOf(char[])

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书香水墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值