String、StringBuffer、StringBuilder

String、StringBuffer、StringBuilder常用方法



StringBuffer、StringBuilder参考手册


1. String参考手册

String类是非常重要的一个类,开发中会经常使用到。

String类用来表示一个字符串,所有类都可以通过toString()方法将该类对象转为字符串。JDK8中String类由final修饰,底层是基于char[ ],并用private final修饰:

private final char value[];

JDK9改为用byte[ ]和一个结束标志来表示字符串,并加上了@Stable注解:

@Stable
private final byte[] value;

private final来修饰,这意味着String的值是不可改变的,每一次的改变其实都是产生了新的String对象。String类中为我们提供了许多方法供我们来操作字符串,接下来就为大家逐一介绍每个方法。



1.1 构造方法


方法名 返回值 功能描述 示例
1、String() String 创建一个空字符串的String对象 示例

2、String(byte[]) String 通过字节数组来创建一个String对象 示例

3、String(byte[],String) String 创建指定编码格式的String对象,其中charsetName是编码格式名称 示例

4、String(byte[] , Charset) String 创建指定字符编码格式的String对象,Charset的实现类就是各种编码格式 示例

5、String(byte[],int) String 已过时 示例

6、String(byte[],int,int) String 截取字节数组指定长度的字符创建String对象 示例

7、String(byte[],int,int,Charset) String 使用byte[]数组来创建String对象 示例

8、String(byte[],int,int,int) String 已过时 示例

9、String(byte[],int,int,String) String 截取指定字符来创建String对象,并指定字符编码 示例

10、String(char[]) String 用char[]类型数组转为String对象 示例

11、String(char[],int,int) String 用指定长度的char字符创建String对象 示例

12、String(int[],int, int) String 使用字符对应的ASCII码来创建String对象 示例

13、String(String) String 新建一个String对象,该String对象是参数的副本 示例

14、String(StringBuffer) String StringBuffer作为参数 示例

15、String(StringBuilder) String StringBuilder作为参数 示例

16、String(char[],boolean) String 不能直接调用 示例


1.2 常用方法​


方法名 返回值 功能描述 示例
1、charAt(int) String 根据索引获取字符串中指定字符 示例

2、codePointAt(int) int 根据索引获取指定字符的ASCII编码,即字符对应的int类型 示例

3、codePointBofore(int) int 根据索引获取指定字符之前的字符的ASCII编码,返回int类型 示例

4、codePointCount() int 获取两索引值之间的字符的个数 示例

5、compareTo(String) int 比较两字符串,出现不相同字符时,返回不相同处两字符的Unicode的差值 示例

6、compareToIngoreCase int 忽略大小写比较两字符串,即A和a视为相同字符,出现不相同字符时,返回不相同处两字符的ASCII的差值 示例

7、concat(String) String 将字符串拼接到该字符串末尾,返回拼接后的字符串,连续调用连续拼接 示例

8、contains(CharSequence) boolean 判断是否包含某一子串 示例

9、contentEquals() boolean 与指定的CharSequence字符序列进行比较,相等返回true 示例

10、contentEquals() boolean 与StringBuffer类型字符串进行比较,内容相等返回true 示例

11、copyValueOf(char[]) String 静态方法,将char[]数据转为字符串,返回String对象 示例

12、copyValueOf() String 截取指定长度的char[]数组转为字符串,返回截取的String对象 示例

13、endsWith(String) boolean 判断是否是以某一字符串结尾,是返回true,反之返回false 示例

14、queals(Object) boolean 比较两字符串是否相同 示例

15、equalsIgnoreCase() boolean 忽略大小写比较两字符串是否相同 示例

16、format() String 给定格式和参数,返回拼接好后的字符串,类似于printf()方法 示例

17、format() String 指定语言环境和地区,给定格式和参数,返回拼接好后的字符串 示例

18、getBytes() byte[] 返回该字符串的byte[ ]数组 示例

19、getChar() void 复制指定长度的字符串到char[]数组中,并指定起始位置 示例

20、indexOf() int 匹配某一字符或字符片段,匹配成功就返回匹配到的字符的第一个字符的下标值 示例

21、lastIndexOf和lastIndexOf int 匹配最后一次出现该字符或字符串,返回其索引 示例

22、 intern() String 保证字符串来自唯一字符串常量池 示例

23、isEmpty() boolean 判断字符串是否为空,是返回true,不是返回false 示例

24、join() String 拼接多个字符,返回拼接后的String字符串 示例

25、length() int 获取String的字符数 示例

26、matches(String) boolean 正则匹配字符串,匹配成功返回true 示例

27、offsetByCodePoints(int , int) int 返回从index开始偏移codePointOffset个字符后的字符的索引(目前还不知使用场景) 示例

28、regionMatches() boolean 判断两字符串的字串是否相等 示例

29、 replace()系列 String 替换指定字符或字串,返回替换后的字符串 示例

30、 split String[] 按照指定分隔符拆分为多个字符串,以String数组形式返回拆分后的结果,分隔符可以以正则表达式的形式来指定; 示例

31、startsWith() boolean 判断是否以某一字符串开头 示例

32、subSequence(int,int) String 返回指定区间的子串 示例

33、substring() String 根据索引截取指定区间的字符串,返回截取的字符串 示例

34、toCharArray() char[] 将String字符串转为char[]数组型,返回转换后的char[]数组 示例

35、toLowerCase() String 将字符串中所有大写字母转为小写字母 示例

36、toString() String 返回本String对象引用 示例

37、toUpperCase() String 将字符串中的小写字母全部转为大写 示例

38、trim() void 去除字符串前后多余的空格 示例

39、valueOf()系列 String 基本类型如char、int、double、float、char[]、long、Object等转为String类型 示例

1.3 构造方法代码

1、String() ⏫

public void StringConstructorTest(){
   
       String str= new String();
       //输出ture,证明它是空字符串
       System.out.println(str.isEmpty());
       System.out.println("".isEmpty());
}

2、String(byte[] byte)⏫

public void StringConstructorTest(){
   
    byte[] bytes = {
   'H','e' ,'l' ,'l' ,'o' ,' ','W' , 'o' , 'r' , 'l' , 'd' , '!' , '!'};
    String str = new String(bytes);
    //输出Hello World!!
    System.out.println(str);
}

3、String(byte[] bytes , String charsetName)⏫

public void StringConstructorTest(){
   
  byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
  String str = null;
  try {
   
        //字符编码为GBK
        str = new String(bytes , "GBK");
  } catch (UnsupportedEncodingException e) {
   
      e.printStackTrace();
  }
   System.out.println(str);
}

4、String(byte[] bytes , Charset charset) ⏫

public void StringConstructorTest(){
   
   byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
   //指定编码
   Charset charset = new GBK();
   String str = new String(bytes , charset);
   System.out.println(str);
}

5、String(byte[] ascii , int hibyte) ⏫

public void StringConstructorTest(){
   
    byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
    String str = new String(bytes , 0);
    //输出Hello World!!
    System.out.println(str);
}

6、String(byte[] bytes , int offset , int length) ⏫

public void StringConstructorTest(){
   
     byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
     //从下标0开始(含),截取8个字符
     String str1 = new String(bytes , 0 , 8);
     //从下标2开始(含),截取8个字符
     String str2 = new String(bytes , 2 , 8);
     //输出Hello Wo
     System.out.println(str1);
     //输出llo Worl
     System.out.println(str2);
}

7、String(byte[] bytes , int offset , int length , Charset cherset) ⏫

public void StringConstructorTest(){
   
     byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
     Charset gbk = new GBK();
     //从下标2开始(含),截取8个字符,指定字符编码为GBK
     String str = new String(bytes , 2 , 8 , gbk);
     //输出llo Worl
     System.out.println(str);
}

8、String(byte[] ascii , int hibyte , int offset , int count)⏫

public void StringConstructorTest(){
   
   /**
    * 8. String(byte[] ascii, int hibyte int offset int count):
    * 已经过时,不能正确的将字节转为字符。
    * */
    byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
    String str = new String(bytes , 0 , 4 , 5);
    System.out.println(str);
}

9、String(byte[] bytes , int offset , int length , String charsetName)⏫

public void StringConstructorTest(){
   
  /**
   * 9.String(byte int int String)
   * */
   byte[] bytes = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
   Charset charset = new GBK();
   //从索引为4开始,截取5个字符
   String str = new String(bytes , 4 , 5 , charset);
   System.out.println(str);
}

10、String(char[] value)⏫

public void StringConstructorTest(){
   
  /**
   * 10.String(char[] value)
   * */
   char[] chars = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
   String str = new String(chars);
   System.out.println(str);
}

11、String(char[] value , int offset , int count)⏫

public void StringConstructorTest(){
   
    /**
     * 11.String(char[] value , int offset , int count)
     * */
     char[] chars = {
   'H','e','l','l','o',' ','W','o','r','l','d','!','!'};
     //截取指定长度的字符,从索引为6(含)开始,截取5个字符
     String str = new String(chars , 6  , 5);
     //输出World
     System.out.println(str);
}

12、String(int[] codePoints , int offset , int count)⏫

/**
 * String(int[] codePoint , int offset , int count):
 * 使用
 * */
 int [] c = {
   65,66,67,68,97,98,99,100};//ASCII码
 String str  = new String(c , 0 , 8);
 //输出ABCDabcd
  System.out.println(str);

13、 String(String original)⏫

/**
 * 13.String(String original):
 * 新建String对象是参数的副本
 * */
String str = new String("Hello World!!");
System.out.println(str);

14、String(StringBuffer buffer)⏫

/**
 * 14.String(StringBuffer buffer):
 * */
StringBuffer stringBuffer = new StringBuffer("Hello World");
String str = new String(stringBuffer);
System.out.println(str);

15、String(StringBuilder builder)⏫

/**
 * 15.String(StringBuilder builder):
 * */
StringBuilder stringBuilder = new StringBuilder("Hello World");
String str = new String(stringBuilder);
System.out.println(str);

16、String(char[] value , boolean share)⏫

/*
 * Package private constructor which shares value array for speed.
 * this constructor is always expected to be called with share==true.
 * a separate constructor is needed because we already have a public
 * String(char[]) constructor that makes a copy of the given char[].
 */
 String(char[] value, boolean share) {
   
     // assert share : "unshared not supported";
     this.value = value;
 }

1.4 常用方法代码


1、charAt(int index)⏫

/**
 * charAt(int index): 根据索引获取字符串中指定字符
 * */
public void charAtTest(){
   
    String str1 = "Hello\u0020World!! \r\n";
    String str2 = "你好\u3000世界!!";
    System.out.print(str1.charAt(4));//o
    System.out.print(str1.charAt(5));//\u0020: 半角空格(英文符)
    System.out.print(str1.charAt(6));//W
    System.out.print(str1.charAt(14));//回车符
    System.out.print(str1.charAt(15));//换行符
    System.out.print(str2.charAt(1));//好
    System.out.print(str2.charAt(2));//\u3000: 全角空格(中文符)
    System.out.print(str2.charAt(3));//世
}

2、codePointAt(int index)⏫

/**
 * codePointAt(int index): 根据索引获取字符串的指定字符的ASCII编码,int类型
 * */
public void codePointAtTest(){
   
    String str1 = "Hallo\u0020World!! \r\n";
    String str2 = "你好\u3000世界!!";
    System.out.
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值