Best way to replace multiple characters in a string?

Replacing two characters I timed all the methods in the current answers along with one extra. With an input string of abc&def#ghi and replacing & -> \& and # -> \#, the fastest way was to chain together the replacements like this: text.replace(‘&’, ‘\&’).replace(‘#’, ‘\#’). Timings for each function: a) 1000000 loops, best of 3: … Read more

JavaScript – Replace all commas in a string [duplicate]

The third parameter of String.prototype.replace() function was never defined as a standard, so most browsers simply do not implement it. The best way is to use regular expression with g (global) flag. var myStr=”this,is,a,test”; var newStr = myStr.replace(/,/g, ‘-‘); console.log( newStr ); // “this-is-a-test” Still have issues? It is important to note, that regular expressions … Read more

How to replace multiple substrings of a string?

Here is a short example that should do the trick with regular expressions: import re rep = {“condition1”: “”, “condition2”: “text”} # define desired replacements here # use these three lines to do the replacement rep = dict((re.escape(k), v) for k, v in rep.iteritems()) #Python 3 renamed dict.iteritems to dict.items so use rep.items() for latest … Read more

Replace a character at a specific index in a string?

String are immutable in Java. You can’t change them. You need to create a new string with the character replaced. String myName = “domanokz”; String newName = myName.substring(0,4)+’x’+myName.substring(5); Or you can use a StringBuilder: StringBuilder myName = new StringBuilder(“domanokz”); myName.setCharAt(4, ‘x’); System.out.println(myName);

How to input a regex in string.replace?

This tested snippet should do it: import re line = re.sub(r”</?\[\d+>”, “”, line) Edit: Here’s a commented version explaining how it works: line = re.sub(r””” (?x) # Use free-spacing mode. < # Match a literal ‘<‘ /? # Optionally match a “https://stackoverflow.com/” \[ # Match a literal ‘[‘ \d+ # Match one or more digits … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)