Tutorials Point


  Java Basics Examples
  Java Tutorial
  Java Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Java Examples - Replacing a String


previous next AddThis Social Bookmark Button


Problem Description:

How to replace a substring inside a string by another one ?

Solution:

This example describes how replace method of java String class can be used to replace character or substring by new one.

public class StringReplaceEmp{
   public static void main(String args[]){
      String str="Hello World";
      System.out.println( str.replace( 'H','W' ) );
      System.out.println( str.replaceFirst("He", "Wa") );
      System.out.println( str.replaceAll("He", "Ha") );
   }
}

Result:

The above code sample will produce the following result.

Wello World
Wallo World
Hallo World


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names