Trillian :
Imagine I want to replace all occurrences of 'A' in a string with '1', 'B' with '2' and 'C' with '3'. I have currently
str.replace('A', '1').replace('B', '2').replace('c', '3')....
I know I can create a map with the chars to replace as keys and the replacements as values and iterate over it replacing each occurrence. I was wondering if there is shorter way using some regex or a method from String class i was not aware of. Something like
str.replaceEach([ABC],[123])
Nexevis :
I don't think there is one you can use from the standard library, but you can use replaceChars
from the apache StringUtils
library, the docs can be found here.
public static String replaceChars(String str,
String searchChars,
String replaceChars)
Replaces multiple characters in a String in one go. This method can also be used to delete characters.
There are some examples listed in the docs that show what you want to do.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=311370&siteId=1