(Flutter / Dart)用字符串中的html标签括号替换所有括号吗?

时间:2018-10-20 18:28:55

标签: string mobile dart flutter

我需要用html标签替换使用某些特殊字符的括号。

示例:

“''test”“变为” test

“ // Example //”变为“ Example

如何在Flutter中使用Dart语言实现这一目标?

1 个答案:

答案 0 :(得分:1)

使用replaceFirst可能会让您想要。

main() {
  String test = "\"test\" //Example//";

  final Map<String, List<String>> map = {
    "\"": ["< b >", "< \/b >"],
    "//": ["< i >", "< /i >"]
  };

  map.forEach((key, mapping) {
    test = test.replaceFirst(key, mapping[0]);
    test = test.replaceFirst(key, mapping[1]);
  });

  print(test);
}