Skip to content

Instantly share code, notes, and snippets.

@funkydevil
Last active November 5, 2023 12:25
Show Gist options
  • Save funkydevil/bf6d290e43536c3594141deb4e981698 to your computer and use it in GitHub Desktop.
Save funkydevil/bf6d290e43536c3594141deb4e981698 to your computer and use it in GitHub Desktop.
Replace all %@ in string

Replace all %@ in string

Created with <3 with dartpad.dev.

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