At the moment I'm doing
Example:
line.replaceAll(",","").replaceAll("cat","dog").replaceAll("football","rugby");
I think that it ugly. Not sure a better way to do this? Maybe loop through a hashmap?
EDIT:
By efficiency I mean better code style and flexibility
var str = "I have a cat, a dog, and a goat."; str = str. replace(/goat/i, "cat"); // now str = "I have a cat, a dog, and a cat." str = str. replace(/dog/i, "goat"); // now str = "I have a cat, a goat, and a cat." str = str.
Use the translate() method to replace multiple different characters. You can create the translation table specified in translate() by the str. maketrans() . Specify a dictionary whose key is the old character and whose value is the new string in the str.
If you press Ctrl+H, it will bring up the Find/Replace dialog box. You can enter what you're looking for, enter what you want to replace it with and press a button. For changing the text color, select the range necessary, and go to Format|Conditional Formatting.
This functionality is already implemented in Commons Lang's StringUtils
class.
StringUtils.replaceEach(String text, String[] searchList, String[] replacementList)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With