【发布时间】:2021-03-16 13:57:50
【问题描述】:
我正在尝试检查字符串变量中字符“-”的索引。尽管变量包含“-”,dart indexof 和 lastIndexof 返回 -1。我还检查了“包含”方法,它同样返回该字符串不包含“-”,尽管它确实包含。代码如下:
String s = "Oh yes, the past can hurt. But the way I see it, you can either run from it or learn from it. – The Lion King";
int x = s.indexOf('-');
print(x);
if(s.contains("-")){
// print("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv............");
print("Yipee string contains -");
}else{
print("string does not contain -");
// the following is the output:
//string does not contain -
}
if(x == -1){
print(x);
// It prints out -1
}else{
print(x);
return s.substring(0,x);
}
看在上帝的份上,我做错了什么?
【问题讨论】:
-
你能不能把字符
–复制到– The Lion King,然后粘贴到你的indexOf(...)和contains(...)。我认为这个角色是不同的。