web123456

Learning Diary - Adjusting the Order of Specified Characters in a String

//1. Creating strings StringBuffer str = new StringBuffer("abc123"); System.out.println("Before Reversal:" + str); //2. Setting the inversion parameter/position int start = 1; int end = 5; //3. will need to reverse the character to intercept, the return value is String String reverse = str.substring(start, end); //4. To perform the reverse operation, you need to convert String to StringBuffer or StringBuilder first. // Because String doesn't have a reverse() method. StringBuffer str2 = new StringBuffer(reverse); //5. Call the reverse method reverse() str2.reverse(); //6. Replace the original character with the reversed character str.replace(start,ent, String.valueOf(str2)); System.out.println("After Reversal:" + str);