提问人:Golden Lion 提问时间:4/1/2022 最后编辑:M. JustinGolden Lion 更新时间:11/18/2023 访问量:50
为什么从单词列表中随机选择元素时,元素末尾会出现空白字符 [已关闭]
Why am I getting an blank character at the end of an element when randomly selecting an element from a word list [closed]
问:
我必须对我选择的单词进行硬编码以避免空白字符
Future<String> getData() async {
try {
return await rootBundle.loadString('text_file/four_words.txt');
} catch (e) {
throw (e.toString());
}
}
Future<String> val = getData();
val.then((value) {
setState(() {
dataString = value;
var elements = dataString.split("\n");
elements.forEach((element) {
wordList.add(element);
//debugPrint(element);
});
_selectNewWord() {
setState(() {
selectedWord = wordList[_random.nextInt(wordList.length)];
selectedWord=selectedWord[0]+selectedWord[1]+selectedWord[2]+selectedWord[3];
var elements2 = selectedWord.split("");
myList.clear();
for (var element in elements2) {
if (element.isNotEmpty) {
myList.add(element);
}
}
});
}
答:
0赞
Golden Lion
4/2/2022
#1
我实现了dataString.replaceAll(“\r\n”, “\n”).split(“\n”),它通过删除导致拆分列表中额外项目的\r解决了这个问题
评论
dataString.replaceAll("\r\n", "\n").split("\n")
element.isNotEmpty
''