我正在为这个应用程序使用 flutter,但我在应用程序的逻辑方面遇到了问题。任何帮助深表感谢。
应用程序目标:通过以下方式将所有输入缩写解码(替换)为单词:
- 用户通过文本框输入文本
-应用程序查找任何缩写(几个)并仅用文本替换缩写。
我能够使用一些缩写来做到这一点,但就我而言,所有缩写都应该在输入文本中,否则它将不起作用,或者第二个索引将不起作用。
我尝试了几种不起作用的方法,我使用 2 个列表来表示 abv 和相应的文本。
这是代码。
List<String> coded = ["GM", "HOT", "YAH"]; //ABV list
List<String> decoded = ["Gmail", "Hotmail", "Yahoo"]; //corresponding list
Map<String, String> map = new Map.fromIterables(coded, decoded);
String txt = "HOT was the best until GM took over"; //input text
void main() {
if ((txt.contains(coded[0]))) { //GM
String result = txt.replaceAll(coded[0], decoded[0]); //Replace with Gmail
print(result);
}
else if ((txt.contains(coded[0])) && (txt.contains(coded[1]))) {
String result = (txt.replaceAll(coded[0], decoded[0]));
(txt.replaceAll(coded[1], decoded[1]));
print(result);
}
else if ((txt.contains(coded[0])) && (txt.contains(coded[1])) && (txt.contains(coded[2]))) {
String result = txt.replaceAll(coded[0], decoded[0]);
txt.replaceAll(coded[1], decoded[1]);
txt.replaceAll(coded[2], decoded[2]);
print(result);
}
else {
print(txt);
}
}
对于其他根据问题标题来到这里的人,请使用replaceAll
:
final original = 'Hello World';
final find = 'World';
final replaceWith = 'Home';
final newString = original.replaceAll(find, replaceWith);
// Hello Home
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)