-
String翻转
1. 要求将字符串指定的部分进行翻转
public class StringHomework {
public static void main(String[] args) {
// 要求将字符串指定的部分进行翻转
// 例如:abcdef ---> a edcb f 1, 4
System.out.print("转换前: ");
String s = "abcdef";
String s1 = null;
System.out.println(s);
try {
System.out.println("=========");
System.out.print("转换后: ");
System.out.println(StringHomework.reverse(s, 1, 4));
} catch (NullPointerException e) {
System.out.println(e.getMessage() + "传入的字符串为null!");
}
}
public static String reverse(String str, int start, int end) throws NullPointerException{
// 先将字符串转换为字符数组
char[] chars = str.toCharArray();
// 判断 str 是否合法
if (str.isEmpty()) {
throw new RuntimeExc