Java String Replace Example

  1. /*
  2. Java String replace example.
  3. This Java String Replace example describes how replace method of java String class
  4. can be used to replace character or substring can be replaced by new one.
  5. */
  6.  
  7. public class JavaStringReplaceExample{
  8.  
  9. public static void main(String args[]){
  10.  
  11. /*
  12.   Java String class defines three methods to replace character or substring from
  13.   the given Java String object.
  14.   1) String replace(int oldChar, int newChar)
  15.   This method replaces a specified character with new character and returns a
  16.   new string object.
  17.   2) String replaceFirst(String regularExpression, String newString)
  18.   Replaces the first substring of this string that matches the given regular
  19.   expression with the given new string.
  20.   3) String replaceAll(String regex, String replacement)
  21.   Replaces the each substring of this string that matches the
  22.   given regular expression with the given new string.
  23.   */
  24.  
  25. String str="Replace Region";
  26.  
  27. /*
  28.   Replaces all occourances of given character with new one and returns new
  29.   String object.
  30.   */
  31. System.out.println( str.replace( 'R','A' ) );
  32.  
  33. /*
  34.   Replaces only first occourances of given String with new one and
  35.   returns new String object.
  36.   */
  37. System.out.println( str.replaceFirst("Re", "Ra") );
  38.  
  39. /*
  40.   Replaces all occourances of given String with new one and returns
  41.   new String object.
  42.   */
  43. System.out.println( str.replaceAll("Re", "Ra") );
  44.  
  45. }
  46.  
  47. }
  48.  
  49. /*
  50.  
  51. OUTPUT of the above given Java String Replace Example would be :
  52.  
  53. Aeplace Aegion
  54. Raplace Region
  55. Raplace Ragion
  56.  
  57. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!