如果您希望用户输入非反转字符串的start
和索引,您可以通过重新映射索引值轻松实现此目的。end
StringBuilder
以下是您要求的 1 的工作示例:
public static void main(String[] args) {
final StringBuilder sb = new StringBuilder("abcdefgh");
final int startIndex = 2;
final int endIndex = 5;
sb.reverse();
final String output = substring(sb.toString(), startIndex, endIndex);
System.out.println("Result: " + output);
}
//Retrieve the substring of a reversed string
private static String substring(String input, int start, int end) {
start = map(start, 0, input.length(), input.length(), 0);
end = map(end, 0, input.length(), input.length(), 0);
String substring = input.substring(end, start);
return substring;
}
//Map value from the min,max range, to the new minRange-maxRange range.
private static int map(float value, float min, float max, float minRange, float maxRange) {
return Math.round(minRange + (maxRange - minRange) * ((value - min) / (max - min)));
}