Swap Characters in String Java

Swap Characters in String Java | Java provides several built-in methods to swap characters in the string, it has made programmers easy by providing such methods. Let us see different ways available to swap characters in a string Java.

Example of swapping characters in a string Java:-

String str = "Know Program";
System.out.println(str);
System.out.println(swap(str, 6, str.length() - 2));

Output:-

Know Program
Know Paogrrm

Observe the code snippet, it has swapped the element in the 6th position ‘r’ with the element at the 10th position ‘a’ which is the length of the string – 2.

Here the swap() function is a user-defined function created for the user’s convenience. We will see the different ways to write this swap() method to swap characters in string Java.

How to Swap Two Characters in a String Java using toCharArray()

To swap two characters in a string in Java we can use the toCharArray() method available in the Java String class. Observe the below code, the swap is done between the first characters of each word that is 0th position is swapped with the 5th position. ‘K’ is swapped with ‘P’. 

Program to Swap Two Characters in a String Java using toCharArray() Method

public class Main {
   public static char[] swap(String string, int i, int j) {
      char chr[] = string.toCharArray();
      char temp = chr[i];
      chr[i] = chr[j];
      chr[j] = temp;
      return chr;
   }

   public static void main(String args[]) {
      String str = "Know Program";
      System.out.println(str);
      System.out.println(swap(str, 0, 5));
   }
}

Output:-

Know Program
Pnow Krogram

Swap Characters in String Java using substring()

Now, we will swap two characters in a string by using the substring() method. As we know the substring() method is a built-in method in java that returns a substring from the given string. 

Program to Swap Characters in a String Java using substring()

public class Main {
   public static String swap(String string, int i, int j) {
      if (j == string.length() - 1) {
         return string.substring(0, i) + string.charAt(j) + 
                string.substring(i + 1, j) + string.charAt(i);
      }

      return string.substring(0, i) + string.charAt(j) + 
             string.substring(i + 1, j) + string.charAt(i)+ 
             string.substring(j + 1, string.length());
   }

   public static void main(String args[]) {
      String str = "Know Program";
      System.out.println(str);
      System.out.println(swap(str, 0, 5));
   }
}

Output:-

Know Program
Pnow Krogram

Swap Characters in String Java using StringBuilder

As we know the string is immutable which means we can not make any change once a string is created, on each and every modification a new String object will be created. Hence we can use the StringBuffer or StringBuilder class to modify the string elements. Now, let us see how to swap characters in string Java we will use StringBuilder Class.

Program to Swap Two Characters in a String Java Using Stringbuilder

public class Main {
   static String stringSwap(String string, int i, int j) {
      StringBuilder sb = new StringBuilder(string);
      sb.setCharAt(i, string.charAt(j));
      sb.setCharAt(j, string.charAt(i));
      return sb.toString();
   }

   public static void main(String args[]) {
      String str = "Know Program";
      System.out.println(str);
      System.out.println(stringSwap(str, 0, 5));
   }
}

Output:-

Know Program
Pnow Krogram

Observe all the above code works the same to swap characters in string Java but the methods and functions used are different.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.