17

I'm using flutter for this app and I'm having trouble with the app's logic. Any help is much appreciated.

App goal: Decode(replace) all input abbreviation to words by: -User inputs text via text box -App looks for any abbreviations(several) and replaces the abbreviation only with text.

I was able to do it will a few abbreviation but with my case all abbreviation should be in the input text or it wouldn't work or the second index wont work. I tried several ways which didn't work, I'm using 2 list for the abv and corresponding text.

Here is the code.

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);
  }
} 

3 Answers 3

37

For others coming here based on the question title, use replaceAll:

final original = 'Hello World';
final find = 'World';
final replaceWith = 'Home';
final newString = original.replaceAll(find, replaceWith);
// Hello Home
Sign up to request clarification or add additional context in comments.

1 Comment

final String text = text.replaceAll("find", "replace");
14

What you want can be done quite easily with fold:

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() {
  final result = map.entries
    .fold(txt, (prev, e) => prev.replaceAll(e.key, e.value));
  print(result);
}

Basically you will iterate on map entries (pairs of key/value). fold takes an initial value (txt in your case) and a function to update the previous value with the current element that is iterated. After each iteration the result has replaced all ABV.

2 Comments

I really appreciate your help. I ran into another problem, the replaceAll method doesn't consider "matching full words" so if the initial text was for example: ""JP CURRENCY IS JPY" and the keys [ "JP",:JPY"] - values ["JAPAN", "YEN"] => the output would be "JAPAN CURRENCY IS JAPANY". I'm not sure whats happening but it seems that it takes the first available key then overwriting with the second. btw all user inputs will be in capital case. I tried replaceAllMapped but that didn't get me anywhere . Thanks.
You need to use RegExp for that usage : prev.replaceAll(RegExp('\\b${RegExp.escape(e.key)}\\b'), e.value) will do what you want.
0

in your terminal do this:

STEP 1 "cd PATH_OF_YOUR_FOLDER"

STEP 2 "sed "s:AAA:BBB:g" FILE_NAME_TO_UPDATE > NEW_FILE_NAME_TO_SAVE_DATA"

that's it.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.