String类

说在前面

public final class String
extends Object
implements Serializable, Comparable, CharSequence
请先了解String中的常量池的存在和字符在java中使用的编码是Unicode 编码等基本知识。
Since: JDK1.0

属性

static Comparator<String> CASE_INSENSITIVE_ORDER  
静态属性,使用时,产生一个实现了Comparator接口的对象。使用方法如下:

	public static void main(String[] args) {
        String[] str = {"q","53","1","a","23"};
        Arrays.sort(str,String.CASE_INSENSITIVE_ORDER);
        System.out.println(Arrays.toString(str));
    }

构造方法

1. String() 
2. String(byte[] bytes) :使用平台的默认字符集解码指定的字节数组,构造一个新的字符串。
3. String(byte[] bytes, Charset charset) :使用指定的字符集解码指定的字节数组,构造一个新的字符串。
4. String(byte[] ascii, int hibyte) Deprecated.(不建议使用)
5. String(byte[] bytes, int offset, int length) :通过使用平台的默认字符集解码指定的字节子数组来构造一个新的字符串。(offset为开始下标包含)
6. String(byte[] bytes, int offset, int length, Charset charset) 
7. String(byte[] ascii, int hibyte, int offset, int count) Deprecated.(不建议使用)
8. String(byte[] bytes, int offset, int length, String charsetName) :使用字符串指定解码集即可,如"GBK"作为charseName参数。
9. String(byte[] bytes, String charsetName) 
10. String(char[] value)   
11. String(char[] value, int offset, int count) 
12. String(int[] codePoints, int offset, int count) :codePoints为Unicode码表示的字符格式,offset为开始位(包含),count为转换长度。
13. String(String original) 
14. String(StringBuffer buffer) 
15. String(StringBuilder builder) 

静态方法

1. static String copyValueOf(char[] data)  :等价于valueOf(),用一个char数组new一个新的String对象返回。
2. static String copyValueOf(char[] data, int offset, int count)  
3. static String join(CharSequence delimiter, CharSequence... elements):返回一个新的字符串,该字符串由CharSequence元素与指定分隔符连接在一起组成。
4. static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) :elements是一个实现了Iterable的对象。直接看实例:下注!
5. static String format(Locale l, String format, Object... args)  :实例下注!
6. static String format(String format, Object... args) :格式化字符串(具体请百度)实例下注!
7. static String valueOf(boolean b)  
8. static String valueOf(char c)  
9. static String valueOf(char[] data)  
10. static String valueOf(char[] data, int offset, int count)  
11. static String valueOf(double d)  
12. static String valueOf(float f)  
13. static String valueOf(int i)  
14. static String valueOf(long l)  
15. static String valueOf(Object obj)  :将obj对象转换为String,返回的是该类的toString格式。

其他方法

1. char charAt(int index) :返回指定索引处的char值。
2. int codePointAt(int index)  :返回指定索引处的字符(Unicode代码)3. int codePointBefore(int index)  :返回指定索引之前的字符(Unicode代码)4. int codePointCount(int beginIndex, int endIndex)  :统计数量的
5. int compareTo(String anotherString)  :比较大小,当前字符串大返回整数;相等返回0;anotherString返回负数。
6. int compareToIgnoreCase(String str)  :比较大小,忽略大小写。
7. String concat(String str)  :将指定的字符串连接到该字符串的末尾。
8. boolean contains(CharSequence s)  :是用来查找子串的。(实现CharSequence的类:CharBuffer, Segment, String, StringBuffer, StringBuilder )
9. boolean contentEquals(CharSequence cs)  :比较是否相等
10. boolean contentEquals(StringBuffer sb) :其实就是上面那个函数。
11. boolean endsWith(String suffix)  :检测是否这个字符串以指定的后缀结束。
12. boolean equals(Object anObject)  :比较两个字符串是否相等,anObject是String的子类。
13. boolean equalsIgnoreCase(String anotherString)  :比较是否相等,忽略大小写。
14. byte[] getBytes()  :使用平台的默认字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。
15. byte[] getBytes(Charset charset)  :使用指定的字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。
16. void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) Deprecated.    
17. byte[] getBytes(String charsetName)  
18. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)  :将此字符串中的字符复制到目标字符数组中。srcBegin包含,srcEnd不包含,dstBegin是复制到dst中的开始下标。
19. int hashCode()  
20. int indexOf(int ch)  :返回此字符串中指定字符的第一个匹配项的索引。
21. int indexOf(int ch, int fromIndex)  :从fromIndex开始找。
22. int indexOf(String str) :找子串,返回子串的第一个字符所在的下标。
23. int indexOf(String str, int fromIndex)  
24. String intern()  :返回字符串对象的规范表示形式。
25. boolean isEmpty() :判断字符串内容是否为空。
26. int lastIndexOf(int ch)  :返回此字符串中指定字符最后一次出现的索引。
27. int lastIndexOf(int ch, int fromIndex)  
28. int lastIndexOf(String str)  
29. int lastIndexOf(String str, int fromIndex)  
30. int length()  
31. boolean matches(String regex)  :判断这个字符串是否匹配给定的正则表达式。
32. int offsetByCodePoints(int index, int codePointOffset)  :《没看懂》
33. boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)  
34. boolean regionMatches(int toffset, String other, int ooffset, int len) :如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。实例下注! 
35. String replace(char oldChar, char newChar)  :返回一个由newChar替换oldChar而得到的字符串。
36. String replace(CharSequence target, CharSequence replacement)  
37. String replaceAll(String regex, String replacement)  :用给定的replacement替换这个字符串中与给定正则表达式匹配的每个子字符串。
38. String replaceFirst(String regex, String replacement) :将匹配给定正则表达式的字符串的第一个子字符串替换为给定的替换。 
39. String[] split(String regex)  :将这个字符串按照给定的正则表达式的匹配进行分割。
40. String[] split(String regex, int limit)  :limit确定分为几份(从左到右先匹配先分)
41. boolean startsWith(String prefix)  :测试此字符串是否以指定的前缀开始。
42. boolean startsWith(String prefix, int toffset)  
43. CharSequence subSequence(int beginIndex, int endIndex)  :返回下标范围的一个CharSequence序列。
44. String substring(int beginIndex)  
45. String substring(int beginIndex, int endIndex)  
46. char[] toCharArray()  
47. String toLowerCase()  
48. String toLowerCase(Locale locale)  
49. String toString()  
50. String toUpperCase()  
51. String toUpperCase(Locale locale)  
52. String trim() :返回值为此字符串的字符串,并删除开头和结尾的空格。

第4个静态方法jion函数

List<String> words = Arrays.asList(new String[] { "Hello", "World", "2019" });
String msg = String.join(" ", words);
System.out.println(msg);


输出结果:
	Hello World 2019

format函数实例

str=String.format("Hi,%s", "王力");



输出结果:

Hi,王力
public static void main(String[] args) {
        Date d = new Date();
        System.out.println(d);
        String str = String.format(Locale.CHINA,"Hi,%tc",d);
        System.out.println(str);
    }

输出结果:
Sun Mar 07 11:20:13 CST 2021
Hi,星期日 三月 07 11:20:13 CST 2021

regionMatches函数

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.runoob.com");
        String Str2 = new String("runoob");
        String Str3 = new String("RUNOOB");

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(4, Str2, 0, 5));

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(4, Str3, 0, 5));

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
    }
}



输出结果:
	返回值 :true
	返回值 :false
	返回值 :true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脸是真的白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值