Net-informations.com
SiteMap  | About    

Java String replace()

Replacing a single character

Java String replace() method replaces all existing occurrences of a character in a String with another character.

Syntax

Replacing char sequences

Java String replaceAll() method replaces all the substrings with the replacement String.

Syntax

Replacing first occurrence

Java String replaceFirst() method replaces the first substring that fits the specified with the replacement String.

Syntax

Example

Output

Regular Expressions

The replaceAll() method allowed Regular Expressions to replace all occurrences of matching char sequences inside a String.

Example

Output

Difference between String replace() and replaceAll()

Java String replace method either takes a pair of char's or a pair of CharSequence . The replace method will replace all occurrences of a char or CharSequence. On the other hand, both String arguments to replaceFirst and replaceAll are regular expressions (regex). In the case of performance, the replace() method is a bit faster than replaceAll() because the replaceAll() first compiles the regex pattern and then matches before finally replacing whereas the replace() simply matches for the provided argument and replaces.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.