Flutter 有没有办法替换字符串中的多个关键字并用它们自己的关键字包装它们?

Flutter 有没有办法替换字符串中的多个关键字并用它们自己的关键字包装它们?,flutter,dart,Flutter,Dart,假设我有一个字符串: typed = "need replace this ap" str = "hello I need to replace this asap" 所以我想要的最终结果是: newStr = "hello I <bold>need</bold> to <bold>replace</bold> <bold>this</bold> as<bold>

假设我有一个字符串:

typed = "need replace this ap"
str = "hello I need to replace this asap"
所以我想要的最终结果是:

newStr = "hello I <bold>need</bold> to <bold>replace</bold> <bold>this</bold> as<bold>ap</bold>"
我希望最终的结果应该是:

newStr = "the <bold>app</bold> is very <bold>applicable</bold> in many <bold>app</bold>lications"
newStr=“该应用程序在许多应用程序中都非常适用”
对吧??这可能吗?

嘿,如果你可以忽略这里奇怪的HTML语法

然后我给你写了一个解决方案

将此代码粘贴到省道板中

更新2(所有这一切都要感谢PSKINK的
str.replaceAllMapped
方法)

replaceWithBoldIfExists(字符串类型,字符串str){
var n=类型化的长度;
列表搜索列表=新列表();
字符串temp=“”;
int i=0;
对于(i=0;i'${m.group(0)}');
更换退货;
}
void main(){
var typed=“需要更换此ap”;
var str=“您好,我需要尽快更换此”;
打印(替换为boldifexists(键入,str));
}

检查
String.replaceAllMapped
/
String.splitMapJoin
方法documentation@fenchai我的解决方案有帮助吗?输出是
该应用程序在许多应用程序中非常适用
-OP wants
该应用程序在许多应用程序中非常适用
我知道,但这可能会给他/她提供方法的概述或想法。想法是使用
String.replaceAllMapped
-简单到这样:
final pattern=RegExp(“适用的应用程序”);final str=“该应用程序在许多应用程序中都非常适用”;最终替换=str.replaceAllMapped(模式,(m)=>'${m.group(0)}');打印(str);打印(“”);打印(替换)-您也可以使用
String.splitMapJoin
方法如果他没有任何要转换的静态字符串,并且他有一组要转换和替换的字符串,那么他将如何为每个字符串编写替换,请想出一种适合任何字符串而不是静态字符串的方法。非常感谢@pskink的想法和str.replaceAllMapped解决方案。
newStr = "the <bold>app</bold> is very <bold>applicable</bold> in many <bold>app</bold>lications"
removeDuplicates(var typed, var str) {
  Map<String, String> m = new Map<String, String>();

  var n = typed.length;
  String ans = "";

  //for storing the "typed" string  (word by word) into a map "m" variable for later searching purpose
  String temp = "";
  int i = 0;
  for (i = 0; i < n; i++) {
    if (typed[i] == " ") {
      m[temp] = temp;
      temp = "";
    } else {
      temp = temp + typed[i];
    }
  }
  
  //for storing the last word of the string "typed", coz loop will never find a space in last of the string
  m[temp] = temp;

  // map variable loop for search from map "m" in the "str" string, and matching if the word is present or not
  var n2 = str.length;
  String temp2 = "";
  for (int j = 0; j < n2; j++) {
    if (str[j] == " ") {
      if (m.containsKey(temp2)) {
      } else {
        ans = ans + " " + temp2; //storing the "temp2" string into "ans" string, everytime it finds a space and if the string is not already present in the map "m"
      }
      temp2 = "";
    } else {
      temp2 = temp2 + str[j];
    }
  }

  //for searching for the last word of the string "str" in map "m", coz loop will never find a space in last of the string,
  if (m.containsKey(temp2)) {
  } else {
    ans = ans + " " + temp2;
  }

  return ans;
}

void main() {
  String typed = "need replace this ap";
  var str = "hello I need to replace this asap";
  String answer = removeDuplicates(typed, str);
  print(answer);
}
removeDuplicates(var typed, var str) {
  Map<String, String> m = new Map<String, String>();

  var n = typed.length;
  String ans = "";

  //for storing the "typed" string  (word by word) into a map "m" variable for later searching purpose
  String temp = "";
  int i = 0;
  for (i = 0; i < n; i++) {
    if (typed[i] == " ") {
      m[temp] = temp;
      temp = "";
    } else {
      temp = temp + typed[i];
    }
  }

  //for storing the last word of the string "typed", coz loop will never find a space in last of the string
  m[temp] = temp;

  print(m);

  // map variable loop for search from map "m" in the "str" string, and matching if the word is present or not
  var n2 = str.length;
  String temp2 = "";
  for (int j = 0; j < n2; j++) {
    if (str[j] == " ") {
      if (m.containsKey(temp2)) {
        temp2 = "<bold>" + temp2 + "</bold> ";
        ans = ans + " " + temp2;
      } else {
        ans = ans +
            " " +
            temp2; //storing the "temp2" string into "ans" string, everytime it finds a space and if the string is not already present in the map "m"
      }
      temp2 = "";
    } else {
      temp2 = temp2 + str[j];
    }
  }

  //for searching for the last word of the string "str" in map "m", coz loop will never find a space in last of the string,
  if (m.containsKey(temp2)) {
    temp2 = "<bold>" + temp2 + "</bold> ";
    temp2 = "";
  } else {
    ans = ans + " " + temp2;
    temp2 = "";
  }

  return ans;
}

void main() {
  var typed = "applicable app";
  var str = "the app is very applicable in many applications";
  String answer = removeDuplicates(typed, str);
  print(answer);
}
replaceWithBoldIfExists(String typed, String str) {
  var n = typed.length;
  List<String> searchList = new List<String>();

  String temp = "";
  int i = 0;
  for (i = 0; i < n; i++) {
    if (typed[i] == " ") {
      searchList.add(temp);
      temp = "";
    } else {
      temp = temp + typed[i];
    }
  }

  searchList.add(temp);

  String pat = searchList.join('|');
  final pattern = RegExp(pat);

  final replaced =
      str.replaceAllMapped(pattern, (m) => '<bold>${m.group(0)}</bold>');
  return replaced;
}

void main() {
  var typed = "need replace this ap";
  var str = "hello I need to replace this asap";
  print(replaceWithBoldIfExists(typed, str));
}