Java 如何将字符串转换为 CharSequence?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1391970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a String to CharSequence?
提问by
How to convert String
to CharSequence
in Java?
如何转换String
为CharSequence
Java?
回答by Jo?o Silva
Since String
IS-ACharSequence
, you can pass a String
wherever you need a CharSequence
, or assign a String
to a CharSequence
:
由于String
IS-ACharSequence
,您可以在String
任何需要 a的地方传递a CharSequence
,或将 a 分配String
给 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
.
如果要将 a 转换CharSequence
为 a String
,只需使用 的toString
每个具体实现都必须实现的方法CharSequence
。
Hope it helps.
希望能帮助到你。
回答by icza
Straight answer:
正经回答:
String s = "Hello World!";
// String => CharSequence conversion:
CharSequence cs = s; // String is already a CharSequence
CharSequence
is an interface, and the String
class implements CharSequence
.
CharSequence
是一个接口,String
类实现了CharSequence
.
回答by Lucas Fialho
You can use
您可以使用
CharSequence[] cs = String[] {"String to CharSequence"};
回答by krmanish007
CharSequence is an interface and String is its one of the implementations other than StringBuilder, StringBuffer and many other.
CharSequence 是一个接口,String 是它的实现之一,除了 StringBuilder、StringBuffer 和许多其他实现。
So, just as you use InterfaceName i = new ItsImplementation()
, you can use CharSequence cs = new String("string")
or simply CharSequence cs = "string";
因此,就像您使用 一样InterfaceName i = new ItsImplementation()
,您可以使用CharSequence cs = new String("string")
或简单地CharSequence cs = "string";
回答by Sarah Messer
Attempting to provide some (possible) context for OP's question by posting my own trouble. I'm working in Scala, but the error messages I'm getting all reference Java types, and the error message reads a lot like the compiler complaining that CharSequence is nota String. I confirmed in the source code that String implements the CharSequence interface, but the error message draws attention to the difference between String and CharSequence while hiding the real source of the trouble:
试图通过发布我自己的麻烦来为 OP 的问题提供一些(可能的)上下文。我在 Scala 中工作,但是我得到所有引用 Java 类型的错误消息,并且错误消息读起来很像编译器抱怨 CharSequence不是字符串。我在源码中确认了String实现了CharSequence接口,但是错误信息在隐藏真正的问题根源的同时,也提请注意String和CharSequence的区别:
scala> cols
res8: Iterable[String] = List(Item, a, b)
scala> val header = String.join(",", cols)
<console>:13: error: overloaded method value join with alternatives:
(x: CharSequence,x: java.lang.Iterable[_ <: CharSequence])String <and>
(x: CharSequence,x: CharSequence*)String
cannot be applied to (String, Iterable[String])
val header = String.join(",", cols)
I was able to fix this problem with the realization that the problem wasn't String / CharSequence, but rather a mismatch between java.lang.Iterable and Scala's built-in Iterable.
我能够解决这个问题,意识到问题不是 String / CharSequence,而是 java.lang.Iterable 和 Scala 的内置 Iterable 之间的不匹配。
scala> val header = String.join(",", coll: _*)
header: String = Item,a,b
My particular problem can also be solved via the answers at Scala: join an iterable of strings
我的特定问题也可以通过Scala的答案解决:加入一个可迭代的字符串
In summary, OP and others who come across similar problems should parse the error messages very closely and see what other type conversions might be involved.
总之,遇到类似问题的 OP 和其他人应该非常仔细地解析错误消息,并查看可能涉及哪些其他类型转换。