In these series, I'll try to provide code fragments for daily problems in string manipulation. First, we will see how to reverse a string. Note that there are -naturally- more than one way to do this.
01 | public class StringUtils { |
02 | public static String reverseString(String string){ |
19 | return str.reverse().toString(); |
24 | public static void main(String[] args){ |
25 | System.out.println(StringUtils.reverseString( "I am your father" )); |
Output is "rehtaf ruoy ma I".
It is possible to use StringBuffer instead of StringBuilder. As I don't need thread safety I'll pick StringBuilder which's faster. Quite easy to reverse a string, right?