在左键盘上加零的数字 [英] Adding number with zero in left pad
问题描述
首先抱歉愚蠢的问题,因为我是java的新手.其实我有一个字符串,其中包含一个数字,我想将其值增加到某个特定数字.我很困惑如何解释.下面是我需要做的例子.
First of all Sorry for foolish question As i am new to java. actually i have a string which contains a number in between i want to increment its value up to some specific number. i am very confused how to explain . below is the example what i need to do.
我有一个字符串,即:-
"03000001da 00000001 666666"
我想像下面那样增加它的值
I have a string i.e :-
"03000001da00000001666666"
and i want to increase its value like below
"03000001da 00000002 666666"
"03000001da 00000003 666666"
"03000001da 00000004 666666"
等
"03000001da00000002666666"
"03000001da00000003666666"
"03000001da00000004666666"
etc.
我在下面编写了一个简单的Java代码:
I wrote a simple java code below:
但其打印错误,即:-
"03000001da 2 666666"
"03000001da 3 666666"
"03000001da 4 666666"
等
but its print wrongly i.e:-
"03000001da2666666"
"03000001da3666666"
"03000001da4666666"
etc.
public class Demo {
public static void main(String[] args) {
String content = "03000001da00000001666666";
String firstIndex = content.substring(0, 10);
String requireIndex = content.substring(10,18);
String lastIndex = content.substring(18, content.length());
for(int i = 0; i <= 10;i++){
System.out.println(firstIndex + (Integer.parseInt(requireIndex)+i) + lastIndex);
}
}
}
需要一些帮助,请帮助我.
need some help please help me.
推荐答案
尝试一下:
public static void main(String[] args) {
String content = "03000001da00000001666666";
String firstIndex = content.substring(0, 10);
String requireIndex = content.substring(10,18);
String lastIndex = content.substring(18);
for(int i = 0; i <= 10;i++){
System.out.printf("%s%08d%s%n",firstIndex, Integer.parseInt(requireIndex)+i, lastIndex);
}
}
这篇关于在左键盘上加零的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!