Created with <3 with dartpad.dev.
Last active
November 5, 2023 12:25
-
-
Save funkydevil/bf6d290e43536c3594141deb4e981698 to your computer and use it in GitHub Desktop.
Replace all %@ in string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// Define the input string with placeholders | |
String inputString = "Hello, %@! How are you, %@, %@?"; | |
// Define an array of replacement strings | |
List<String> replacements = ["Alice", "Bob"]; | |
print(replaceInString(inputString, replacements)); | |
} | |
String replaceInString(String inputString, List<String> replacements) { | |
String result = inputString.replaceAllMapped(RegExp(r'%@'), (match) { | |
if (replacements.isEmpty) { | |
return "null"; | |
} else { | |
return replacements.removeAt(0); | |
} | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment