我试图比较2个字符串,但是我的比较总是失败。作为参考,一个字符串是我从手机存储中获得的文件名,它看起来像以撇号结束,尽管它在任何地方都不可见。
请考虑以下省道代码:
import 'dart:convert';
void main() {
const Utf8Codec utf8 = Utf8Codec();
String input = 'chatnum.txt';
String stringwithapostrophe = 'chatnum.txt'';
String compInput = utf8.encode(input).toString();
String compComp = utf8.encode(stringwithapostrophe).toString();
print (compInput);
print (compComp);
if (compInput == compComp) {
print ('Yes it matches');
} else {
print ('No it does not');
}
}
这个输出结果是:
[99, 104, 97, 116, 110, 117, 109, 46, 116, 120, 116]
[99, 104, 97, 116, 110, 117, 109, 46, 116, 120, 116, 39]
No it does not
那么如何从字符串中删除最后一个撇号呢?我试过.removeAt
和.removeLast
。但是我就是打不开。
我对它应用了正则表达式。
String filenametosend = (basename(f.toString()))
.replaceAll(RegExp(r"[-!$%^&*()+|~=`{}#@[]:;'’<>?,/"
'"”'
"]"), '');
也是这样:
final apostrophe = ''';
final length = stringwithapostrophe.length;
if (length > 0 && stringwithapostrophe[length - 1] == apostrophe) {
stringwithapostrophe = stringwithapostrophe.substring(0, length - 1);
}
或者这样(remove all):
final apostrophe = ''';
stringwithapostrophe = stringwithapostrophe.replaceAll(apostrophe, '');
Remove (any) last:
final length = stringwithapostrophe.length;
stringwithapostrophe = length > 0
? stringwithapostrophe.substring(0, length - 1)
: stringwithapostrophe;