I'd like to replace all instances of a substring in a string but String.replaceAll() only accepts a pattern. The string that I have came from a previous match. Is it possible to add escapes to the pattern that I have or is there a version of replaceAll() in another class which accepts a literal string instead of a pattern?
            Asked
            
        
        
            Active
            
        
            Viewed 7.8k times
        
    141
            
            
        
        user2428118
        
- 7,935
 - 4
 - 45
 - 72
 
        dromodel
        
- 9,581
 - 12
 - 47
 - 65
 
- 
                    possible duplicate of [Backslash problem with String.replaceAll](http://stackoverflow.com/questions/1701839/backslash-problem-with-string-replaceall) – McDowell May 26 '11 at 08:40
 
2 Answers
230
            Just use String.replace(CharSequence,CharSequence) rather than replaceAll.
NB: replace doesn't just replace the first occurrence, it replaces all occurrences, like replaceAll.
        Sandra Rossi
        
- 11,934
 - 5
 - 22
 - 48
 
        Jon Skeet
        
- 1,421,763
 - 867
 - 9,128
 - 9,194
 
- 
                    25This doesn't just replace the first? Weird they called it "replaceAll" instead of "replaceRegex". – Magic Octopus Urn Mar 28 '18 at 20:13
 - 
                    9```System.out.println("hello world, hello life, hello you".replace("hello","hi"));``` returns ```"hi world, hi life, hi you"```. – Thiago Mata Aug 27 '18 at 02:29
 - 
                    23@MagicOctopusUrn: Yes, I agree it was very poor naming - it's caused a lot of confusion over time. – Jon Skeet Aug 27 '18 at 07:15
 - 
                    very bad naming of the methods. Why do they(sun/oracle) make simple things complicated – Stunner Jun 16 '20 at 02:49
 - 
                    3I believe the naming of `replaceAll` (regex method) was to differentiate from `replaceFirst` (regex method), not so much to differentiate from `replace` (non regex method). – JohnRDOrazio Oct 30 '20 at 14:00
 - 
                    When I try that, why is Android Studio bleating "'replace()' could be replaced with compiled 'java.util.regex.Pattern' construct", as if replace() used a regular expression, when the internal documentation for replace() says "literal target sequence"? – Phlip Feb 19 '21 at 14:50
 - 
                    @Phlip: It may well be that a compiled pattern would be more efficient than using `replace()`. I don't know the details, but that seems entirely plausible. (I would also suggest trying to use dismissive language like "bleating" to describe behavior.) – Jon Skeet Feb 19 '21 at 14:57
 
106
            
            
        The method to add escapes is Pattern.quote().
String replaced = myString.replaceAll(Pattern.quote(matchingStr), replacementStr)
But as Jon says you can just use replace().  Despite the fact that it deviates from the replaceAll name, it does replace all occurrences just like replaceAll().
        Mark Peters
        
- 80,126
 - 17
 - 159
 - 190
 
- 
                    1Works perfectly if you havea "$" in your matchingStr for example. – Julien Lafont Jul 16 '15 at 09:29
 - 
                    rather Pattern.compile(); Pattern.quote() brings undesirable results – Pavlo Zvarych Jun 14 '16 at 11:58
 - 
                    1@PavloZvarych: `Pattern.compile()` compiles the string as a regular expression, meaning the special characters will be given the special meaning. That's the complete opposite of what `Pattern.quote()` does, and what the OP was asking for (`quote()` says, "treat the string as a literal"). Maybe you could expand on what "undesirable results" you're talking about. – Mark Peters Jun 14 '16 at 13:41
 - 
                    
 - 
                    
 - 
                    @MarkPeters, to be honest I was passing "$aa +" to Pattern.quote() method and was expected that I will get the same String as a result after replaceAll() method call. I just needed the same result at the output. Pattern.compile() as it seems helped. I even don't know what to do with such a "\Q$1\E" String. – Pavlo Zvarych Jun 14 '16 at 18:21
 - 
                    1@PavloZvarych: If you're trying to quote the replacement (and not the search pattern), you want to use `Matcher.quoteReplacement("$aa +")`. `Pattern.compile()` produces a `Pattern`, not a `String`, so it's unclear to me how you are even using it in `replaceAll`. – Mark Peters Jun 14 '16 at 18:43
 - 
                    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114668/discussion-between-pavlo-zvarych-and-mark-peters). – Pavlo Zvarych Jun 14 '16 at 19:05