0

Is there a native method in Java to do character swapping inside Strings. I mean, I need to write a function like this everytime and its pretty boring:


    public static String modifyString(String str,int x,int y){

    char arr[]=str.toCharArray();
    char t= arr[x];
    arr[x]=arr[y];
    arr[y]=t;

    String s= new String(arr);
    return s;

}

PermGenError
  • 45,977
  • 8
  • 87
  • 106
OneMoreError
  • 7,518
  • 20
  • 73
  • 112

1 Answers1

1

No, the String class does not contain a method for swapping characters. Unfortunately, you'll either need to roll your own, or look into using a third party library.

mre
  • 43,520
  • 33
  • 120
  • 170