【问题标题】:How to increment "01" as "02" ,"03"...."10","11" in java?如何在java中将“01”递增为“02”,“03”......“10”,“11”?
【发布时间】:2021-08-25 21:38:21
【问题描述】:

我在 selenium 中有一个 x-path,其中包含 p01 到 p15 的动态值。如果是这样,如果我增加它应该像这样通过 02,03,04,05,06,07,08,09,10,11,12 。

但我找不到代码来增加前缀为 0 的值。

【问题讨论】:

    标签: java selenium automation


    【解决方案1】:

    我不确定请求的完整性质,因为缺少一些信息,但您可以在 Java 中使用 String 格式将前导零添加到整数。

    public class Main {
    
        public static void main(String[] args) {
            String formatted = String.format("%02d", 5);
            String formatted1 = String.format("%02d", 11);
            System.out.println(formatted);
        }
    }
    

    Add leading zeroes to number in Java?

    【讨论】:

      【解决方案2】:

      您可以使用一个简单的函数来执行此操作,然后在循环中使用它:

      public getPathValue(int x)
      {
         return x < 10 ? "p0" + x : "p" + x;
      }
      

      如果我们用值 1 => getPathValue(1) 作为返回值来调用它,我们将得到 p01

      如果我们用 10 或更大的值调用它 => getPathValue(10) 作为返回值,我们将得到 p10

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-28
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多