下面是一个用Java实现的方法,可以实现将字符串中的字符倒序输出,但是符号保持原位置:

public class Main {
    public static void main(String[] args) {
        String str = "abcd+efghi";
        String reversedStr = reverseString(str);
        System.out.println(reversedStr);
    }

    public static String reverseString(String str) {
        StringBuilder reversedStr = new StringBuilder();
        StringBuilder tempStr = new StringBuilder();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '+') {
                reversedStr.append(c);
                reversedStr.append(tempStr.reverse());
                tempStr.setLength(0);
            } else {
                tempStr.append(c);
            }
        }

        reversedStr.append(tempStr.reverse());
        return reversedStr.toString();
    }
}

运行结果为:

ihgf+edcba
用java写一个方法要求把字符串abcd+efghi输出为ihgf+edcba。字符倒序输出但是符号保持原位置

原文地址: https://www.cveoy.top/t/topic/jdec 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录