Flutter dart中字符串方法的奇怪行为
我试图检查字符串变量中字符“-”的索引。尽管变量包含“-”,但dart indexof和lastIndexof返回-1。我还检查了“contain”方法,它同样返回字符串不包含“-”,尽管它确实包含“-”。代码如下:Flutter dart中字符串方法的奇怪行为,flutter,dart,Flutter,Dart,我试图检查字符串变量中字符“-”的索引。尽管变量包含“-”,但dart indexof和lastIndexof返回-1。我还检查了“contain”方法,它同样返回字符串不包含“-”,尽管它确实包含“-”。代码如下: 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";
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);
}
看在上帝的份上,我做错了什么?您正在搜索的字符
-
与文本中的字符-
不同。请注意两者之间的长度差异。您正在搜索的字符-
与文本中的字符-
不同。请注意两者之间的长度差异。字符串中的字符是一个短划线,略宽于连字符(-),但窄于em短划线(-)。
由于此原因,contains函数无法检测到连字符,因为存在en破折号。字符串中的字符是一个en破折号,略宽于连字符(-),但窄于em破折号(-)。
因此,contains函数无法检测连字符,因为存在连字符。
'-'
和'-'
是两个不同的字符。看看第一个有多长
void main(){
最终str='–-';
打印(str.codeunit(0));//8211(短划线)
打印(str.codeunit(1));//45(连字符)
}
如果要查找其中一个,还可以使用:
在下面的代码示例中,str1
和str2
几乎相同。我刚刚颠倒了'-'
和'-'
的位置
void main(){
最终str1='123456–78-90';
最终str2='123456-78–90';
最终regExp=regExp(r'[-]');
打印(regExp.firstMatch(str1)?.start);//6
打印(regExp.firstMatch(str2)?.start);//6
}
'-'
和'-'
是两个不同的字符。看看第一个有多长
void main(){
最终str='–-';
打印(str.codeunit(0));//8211(短划线)
打印(str.codeunit(1));//45(连字符)
}
如果要查找其中一个,还可以使用:
在下面的代码示例中,str1
和str2
几乎相同。我刚刚颠倒了'-'
和'-'
的位置
void main(){
最终str1='123456–78-90';
最终str2='123456-78–90';
最终regExp=regExp(r'[-]');
打印(regExp.firstMatch(str1)?.start);//6
打印(regExp.firstMatch(str2)?.start);//6
}
您能否复制–狮子王中的字符-
,然后将其粘贴到(…)
和包含(…)
的索引中。我认为这个字符是不同的。你能复制《狮子王》中的字符-
,然后粘贴到(…)
和包含(…)
的索引中吗。我认为那个角色是不同的。这会有用的!谢谢你,伙计。这很有效。这两个标志看起来很像我的眼睛。再次感谢你的帮助。这会有用的!谢谢你,伙计。这很有效。这两个标志看起来很像我的眼睛。再次感谢你的帮助。