Question

This question already has an answer here:

Can somebody please show me a bit of code to convert a string to charsequence?

Was it helpful?

Solution

String implements the Interface CharSequence, so String is a CharSequence.

And you may never instantiate interfaces. Wherever CharSequence is required, String will fit.

OTHER TIPS

Try this:

// final CharSequence cs = mEditText.getText();    
final CharSequence cs = "Hi how are u";

String[] vals = cs.toString().split(" ");

for(int i=0;i<vals.length;i++) {
    System.out.println("args values...."+i+""+vals[i].toString());          
}

As MByD pointed, String implements CharSequence, so its already a CharSequence. But if you want to convert CharSequence to String, Here is the code:

CharSequence cs = "string";
String s = cs.toString();
foo(s); // prints "string"

public void foo(CharSequence cs) { 
  System.out.println(cs);
}

The SO thread can be found here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top