首页 > 代码库 > 算法-倒置字符串

算法-倒置字符串

/**
 * 
 * @description
 *    <p>String utils</p>
 * @author Czp
 * @version 1.0(2014-5-9)
 *
 */
public class StringUtil {
    
    /**
     * @description:
     *     <p>反转字符串,如果参数为null或为空窜将返回原对象</p> 
     * @param str
     * @return
     */
    public static String reverseString(String str) {
        if (str == null || str.isEmpty())
            return str;
        char[] arr = str.toCharArray();
        int len = arr.length - 1;
        for (int i = 0; i < len; i++, len--) {
            char tmp = arr[i];
            arr[i] = arr[len];
            arr[len] = tmp;
        }
        return new String(arr);
    }
}