如果字符串内部有数字,则将数字平方和 return 作为字符串。我如何用平方数 Java 替换旧数字

If the String has number inside then square the number and return as a String. How do i replace old number with the squared number Java

所以它应该像那样工作

“a44b”上的 squareDigits(“a22b”)

“a81b4”上的 squareDigits(“a9b2”)

到目前为止我得到了什么:

public class Code {
    public static void main(String[] args) {
        String ok = "a2b9";
        System.out.println(squareDigits(ok)); // "a81b4"
    }

  public static String squareDigits(String s) {
        char[] pena = s.toCharArray();
        int aboba;
        for (char c : pena) {
            if (Character.isDigit(c)) {
                String s1 = Character.toString(c);
                int b = Integer.parseInt(s1);
                aboba = b * b;
                System.out.println(aboba);
            }
        }
        return "";
        }
    }

我只是初学者,这对我很有帮助

您可以使用 StringBuffer 并遍历 char 数组并检查 char 是否为数字然后将其平方并放入字符串缓冲区,如果不是 char 则按原样放入字符串缓冲区。

     public static void main(String[] args) {
        System.out.println(squareDigits("a22b"));
        System.out.println(squareDigits("a9b2"));
    }

    public static String squareDigits(String s) {
        char[] pena = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char c : pena) {
            if (Character.isDigit(c)) {
                String s1 = Character.toString(c);
                int b = Integer.parseInt(s1);
                sb.append(b * b);
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

您可以像这样使用 StringBuilder 轻松做到这一点:

public static void main(String[] args) {
    System.out.println(squareDigits("a22b")); // "a44b"
    System.out.println(squareDigits("a2b9")); // "a4b81"
    System.out.println(squareDigits("a55b")); // "a2525b"
}

public static String squareDigits(String s) {
    StringBuilder result = new StringBuilder();
    s.chars().forEach(c -> result.append(addDigitSquaredIfDigit(c)));

    return result.toString();
}

private static String addDigitSquaredIfDigit(int charAsDigit) {
    char c = (char) charAsDigit;
    return Character.isDigit(c) ? getSquaredDigitString(c) : String.valueOf(c);
}

private static String getSquaredDigitString(char c) {
    int digit = Integer.parseInt(Character.toString(c));

    return Integer.toString(digit * digit);
}

也试试。

public static void main(String[] args) {
    String ok = "a2b9";
    System.out.println(squareDigits(ok)); // "a81b4"
}

public static String squareDigits(String s) {
    StringBuilder sb = new StringBuilder();
    String[] chars = s.split("");
    for (String str : chars){
        if (isNumber(str)){
            int num = Integer.valueOf(str);
            Double square = Math.pow(num, 2);
            sb.append(square.intValue());
        } else {
            sb.append(str);
        }
    }
    return sb.toString();
}

private static boolean isNumber(String str) {
    try {
        Integer.valueOf(str);
        return true;
    } catch (Exception e) {
        return false;
    }
}

一种纯函数式的方式(可能不是最优的方式):

public static String squareDigits(String s) {
      char[] pena = s.toCharArray();
      return IntStream.range(0, pena.length).mapToObj(i -> pena[i]).map(it -> {
        String c = it.toString();
        try {
          int intVal = Integer.parseInt(c);
          return "" + (intVal * intVal);
        } catch (Exception e) {
          return c;
        }
      }).reduce("", String::concat);
}