Java之CharSequence

CharSequence:

是一个描述字符串结构的接口,有(并非只有)三种常用的子类:

  - String

  - StringBuffer

  - StringBuilder

String

StringBuffer

StringBuilder

public final class String

extends Object
implements Serializable,Comparable <String>,CharSequence

public final class StringBuffer

 

extends Object
implements Serializable,CharSequence

public final class StringBuilder

extends Object
implements Serializable,CharSequence

Java之CharSequence

 

 

现在只要有字符串就可以对CharSequence进行实例化:

 

  

CharSequence cs = "www.baicu.com";  // 子类型对象向父接口转型
        System.out.println(cs );
Java之CharSequence

 

CharSequence本身是一个接口,在该接口之中也定义有如下操作方法:

Java之CharSequence

 

 

  - 获取指定索引字符:public char charAt(int index);

 

  - 获取字符串长度:public int length();

 

  

- 截取部分字符串: public CharSequence subSequence(int start, int end);

        CharSequence sub = cs.subSequence(4,8);
        System.out.println(sub);
Java之CharSequence

 

 

 

只要见到了CharSequence描述的就是字符串。

 

上一篇:CSRobot gen来了


下一篇:对于Maven管理的项目制定虚拟目录