Java 如何将 CharSequence 与字符串进行比较 - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18690835/
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 compare a CharSequence to a String - Android
提问by
I have a class that places a text into an arrayList. Then it makes all the variables into CharSequences, when I try to compare the variables to a String such as == "test"; it doesn't work here is my code that I use to get the variables
我有一个将文本放入 arrayList 的类。然后,当我尝试将变量与诸如 == "test"; 之类的字符串进行比较时,它将所有变量转换为 CharSequences; 它在这里不起作用是我用来获取变量的代码
class Item {
String descs;
public Item (String descs){
this.descs = descs;
}
public CharSequence getDescs() {
return descs;
}
}
This is the code that compares it to the String
这是将它与字符串进行比较的代码
if(p.getDescs().toString() == "trash"){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
I know for a fact that p.getDescs() is equal to trash because when it sets the text for descsView it is set to trash. SO why doesn't the first if statement work?
我知道 p.getDescs() 等于垃圾,因为当它为 descsView 设置文本时,它被设置为垃圾。那么为什么第一个 if 语句不起作用?
采纳答案by Raghunandan
Use .equals
or .equalsIgnoreCase
to compare strings
使用.equals
或.equalsIgnoreCase
比较字符串
if(p.getDescs().toString().equals("trash"))
Also check this
也检查这个
回答by Kon
If descs is your CharSequence variable:
如果 descs 是您的 CharSequence 变量:
descs.toString().equals("SomeString");
回答by wattostudios
You need to use the equals()
method for String comparison...
您需要使用该equals()
方法进行字符串比较...
if(p.getDescs().toString().equals("trash")){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
Refer to How do I compare strings in Java?for more information.
请参阅如何比较 Java 中的字符串?想要查询更多的信息。
回答by Trikaldarshi
The Best method should be like thi s
最好的方法应该是这样的
if("trash".equals(p.getDescs()){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
Alternatively if comparison is not case sensitive then
或者,如果比较不区分大小写,则
if("trash".equalsIgnoreCase(p.getDescs()){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
The function
功能
<String>.equalus(Object)
Compare any string varialbe to value of Object varialble
将任何字符串变量与对象变量的值进行比较
Note:Because the value of p.getDescs()
could be null
注意:因为 的值p.getDescs()
可能为空
so using
所以使用
if(p.getDescs().toString().equals("trash")){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
may through erroron Runtime
可能通过运行时错误
following links may help
以下链接可能会有所帮助
回答by Suragch
Intro
介绍
A CharSequence
is a general type that includes other types like String
, StringBuilder
, SpannableString
, and others. If you are comparing two strings you can just use .equals
(see this question). But if you are comparing a String
to a CharSequence
, you have to decide what it is you want to compare, since you might be comparing a String
to a StringBuilder
or a SpannableString
or something else.
ACharSequence
是一种通用类型,包括其他类型,如String
、StringBuilder
、SpannableString
等。如果您正在比较两个字符串,则可以使用.equals
(请参阅此问题)。但是,如果您将 aString
与 a进行比较CharSequence
,则必须决定要比较的是什么,因为您可能正在将 aString
与 aStringBuilder
或 aSpannableString
或其他内容进行比较。
Compare content only
仅比较内容
If you only want to compare the content (ie, text) of a String
to the content of the CharSequence
, but you don't care about the type, then you can use String.contentEquals()
.
如果只想比较 a 的内容(即文本)和String
的内容,CharSequence
而不关心类型,那么可以使用String.contentEquals()
.
String myString = "hello";
CharSequence myCharSequence = new StringBuilder("hello");
boolean areEqual = myString.contentEquals(myCharSequence); // true
Compare content and type
比较内容和类型
If you also want to make sure the types are the same in addition to the content, then use String.equals()
.
如果您还想确保除内容之外的类型相同,请使用String.equals()
.
String myString = "hello";
CharSequence myCharSequence = new StringBuilder("hello");
boolean areEqual = myString.equals(myCharSequence); // false
Here it is again with the same types.
这里又是相同的类型。
String myString = "hello";
CharSequence myCharSequence = "hello"; // String
boolean areEqual = myString.equals(myCharSequence); // true
See also
也可以看看
回答by Ovaflo
The currently accepted answer
目前接受的答案
if(p.getDescs().toString().equals("trash"))
can be simplified to
可以简化为
if("trash".contentEquals(p.getDescs()))
Suragch's "Intro" answer gives the details. This is the way to directly compare a CharSequence to a String, which is what the OP asked for.
Suragch 的“介绍”回答给出了详细信息。这是直接将 CharSequence 与 String 进行比较的方法,这正是 OP 所要求的。