I have to reverse the given string and parse it with "-".
for eg. INPUT: abcde
OUTPUT : e-d-c-b-a
Any simple solutions?
>Solution :
Using StringBuilder#reverse we can try:
String input = "abcde";
StringBuilder sb = new StringBuilder(input);
String output = sb.reverse().toString().replaceAll("(?<=.)(?=.)", "-");
System.out.println(output); // e-d-c-b-a
For an explanation on the regex pattern used in String#replaceAll above, it aims to match every position in the string where there are letters on both side:
(?<=.) assert that some character precedes
(?=.) assert that some character follows