How to convert a String to CharSequence?

后端 未结 5 714
别跟我提以往
别跟我提以往 2020-11-28 20:06

How to convert String to CharSequence in Java?

5条回答
  •  醉酒成梦
    2020-11-28 20:32

    Since String IS-A CharSequence, you can pass a String wherever you need a CharSequence, or assign a String to a CharSequence:

    CharSequence cs = "string";
    String s = cs.toString();
    foo(s); // prints "string"
    
    public void foo(CharSequence cs) { 
      System.out.println(cs);
    }
    

    If you want to convert a CharSequence to a String, just use the toString method that must be implemented by every concrete implementation of CharSequence.

    Hope it helps.

提交回复
热议问题