【问题标题】:String Alias id and int value can be added?String Alias id 和 int 值可以加吗?
【发布时间】:2021-10-10 17:58:26
【问题描述】:

String a=A000001; String b = (A000001) + 1; b=A000002;

我想添加两个值。我的第一个值是字符串别名,第二个是 int 1。 有可能吗?

【问题讨论】:

  • 将字符串视为十六进制?当然,这不适用于 K000000 + 1. Base36?
  • 不明白你的意思

标签: java string integer addition


【解决方案1】:

类似

String s = "(A000001) + 1";
String[] operands = s.split(" ");
int a = Integer.parseInt(operands[0].replaceAll("\\W", ""), 16);
int b = Integer.parseInt(operands[operands.length-1].replaceAll("\\W", ""), 16);
//Assume addition or you need to parse and identify the operator
int sum = a + b;
System.out.println(Integer.toString(sum, 16).toUpperCase());

【讨论】:

  • 这完全是另一个问题。请接受这个答案,然后再问另一个
【解决方案2】:

或者只是将它们用作整数 并将整数格式化为十六进制。 更容易增加值。

        int a = 0xA000001;
        int b = a + 1;
        System.out.printf("%X", b);

【讨论】:

  • 需要一个变量类型是字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-19
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多