Как сделать несколько обновлений строки в al oop? - PullRequest
0 голосов
/ 13 марта 2020

Я хочу обновить строку с разметкой для выбранных слов. Эти выбранные слова в списке. Я перебираю список и заменяю работу в строке разметкой.

Однако каждый l oop заменяет всю строку. Я занимался этим некоторое время, и мне нужна другая перспектива.

//String to be markedup
mainText = "Hi there how are you";

//List of words to be marked up are
distinctWords = ['Hi', 'are', 'you'];

 //Iterate though words to me marked up
distinctWords.forEach((word) {
  updatedString = mainText.replaceFirst(RegExp(word), '$word`'); 
 });

 print(updatedString);

Какой подход к этому будет работать, чтобы все три слова были обновлены в одной строке?

Заранее спасибо.

1 Ответ

0 голосов
/ 13 марта 2020

вы должны обновить updatedString примерно так:

//String to be markedup
mainText = "Hi there how are you";

//List of words to be marked up are
distinctWords = ['Hi', 'are', 'you'];
updatedString = mainText;                                             // <- added
 //Iterate though words to me marked up
distinctWords.forEach((word) {
  updatedString = updatedString.replaceFirst(RegExp(word), '$word`'); // <- edited
 });

 print(updatedString);

или

//String to be markedup
mainText = "Hi there how are you";

//List of words to be marked up are
distinctWords = ['Hi', 'are', 'you'];
 //Iterate though words to me marked up
distinctWords.forEach((word) {
mainText = mainText;
  updatedString = mainText.replaceFirst(RegExp(word), '$word`'); // <- edited
 });

 print(updatedString);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...