Java-Replacement in a String

replace

The replace( ) method of the String class enables us to replace every occurrence of one character with another character.

replaceFirst

The replaceFirst( ) method of the String class enables us to replace first occurrence of substring with another substring.

replaceAll

The capability of replaceAll( ) makes the complex search of substrings possible within a string object. After a search, each matched substring is replaced with the given replacement.

Program

1

Program Source

public class Javaapp {
    
    public static void main(String[] args){
        
        String str1 = new String("Jeve Jeve Jeve");
        str1 = str1.replace('e', 'a');
        
        String str2 = new String("deva deva deva");
        str2 = str2.replaceFirst("de","Ja");
        
        String str3 = new String("deva deva deva");
        str3 = str3.replaceAll("de","Ja");
        
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
   }
}

Leave a Comment