Showing posts with label String Replace. Show all posts
Showing posts with label String Replace. Show all posts

String replace in java

string replace in java

We all know that String class is a immutable and values once stored will not be changed. So what is String replace and how its working in java? basically whenever we change are replace the value in String new String Object will be created.
And also have to know that every-time it won't create String Object, only when string change happen in string value then it will create new String Object otherwise it will return same String Object. Totally there are 4 types of replace methods are there in Java,

public String replace(char oldChar, char newChar)
- Used to replace matching character in a String and returns the new String Object. 

public String replace(CharSequence target, CharSequence replacement)
- Used to replace set of character sequence in a String and returns the new String Object.  

public String replaceAll(String regex, String replacement)
- Used to replace all matching pattern in String using regular expression or either simple string and returns the new String Object.  

public String replaceFirst(String regex, String replacement)
- This method is simplar to above method but only used to replace first matching pattern in String using regular expression or either simple string and returns the new String Object.

Important:

  • As we seen above if there is not actual replace then these all methods will return same String Object instead of new String Object.
  • All these methods are Case Sensitive and will replace only with matching case characters.

First lets see simple example for calling replace method and there's no actual replace, so we need to get same String Object.



public class StringReplaceTest {

 public static void main(String[] args) {
  
  String source = "Hello Java";
  
  String str2 = source.replace("hi", "hello");
  
  //By using == operator we can test is it same Object
  if(source == str2){
   System.out.println("First Compare - Both are same String Object");
  }else{
   System.out.println("First Compare - Both are NOT same String Object");
  }
  
  /************************************************/
  
  String str3 = source.replace("Hello", "Hello");
  
  //By using == operator we can test is it same Object
  if(source == str3){
   System.out.println("Second Compare - Both are same String Object");
  }else{
   System.out.println("Second Compare - Both are NOT same String Object");
  }
 }
}


OUTPUT:


First Compare - Both are same String Object
Second Compare - Both are NOT same String Object



In above example we are trying to replace string "hi" with string"hello" where "hi" is not present in source string. So replace method will return same String Object instead of new String in first compare. 
In second replace we trying to replace "Hello" with same same string "Hello" where "Hello" present in source string. So replace method will return new String Object. 


Now lets see simple examples for all 4 replace methods in java 


public class StringReplaceTest {

 public static void main(String[] args) {
  
  String source = "Hello Java Hello";
  
  // Replace character with new character
  String str1 = source.replace('H', 'S');
  System.out.println("\nMethod 1 : "+str1);
  
  // Replace character sequence with new character sequence
  CharSequence old = "Java";
  CharSequence _new = "String";
  
  String str2 = source.replace(old, _new);
  System.out.println("\nMethod 2 : "+str2);
  
  // Replace all matching pattern
  String str3 = source.replaceAll("el", "al");
  System.out.println("\nMethod 3 : "+str3);
  
  // Replace any matching pattern at beginning of the line
  String str4 = source.replaceAll("^H", "S");
  System.out.println("Method 3 : "+str4);
  
  // Replace only first matching pattern
  String str5 = source.replaceFirst("Hello", "Hi");
  System.out.println("\nMethod 4 : "+str5);
 }
}

OUTPUT:


Method 1 : Sello Java Sello

Method 2 : Hello String Hello

Method 3 : Hallo Java Hallo
Method 3 : Sello Java Hello

Method 4 : Hi Java Hello