java中是否有一个类似于SUBSTRING函数但用于整数的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24641882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a function in java that works like SUBSTRING function but for integers?
提问by user3818144
is there a function in java that works like SUBSTRING function but for integers. Like for example the user's input is 456789, I want to break it into two part and put them into different variable. and divide them. for example,
java中是否有一个类似于SUBSTRING函数但用于整数的函数。例如,用户的输入是 456789,我想将它分成两部分并将它们放入不同的变量中。并将它们分开。例如,
user's input : 456789
用户输入:456789
the first 3 numbers will be in variable A.
前 3 个数字将在变量 A 中。
the last 3 numbers will be in variable B.
最后 3 个数字将在变量 B 中。
pass = A/B;
通过 = A/B;
can someone help me how can I do this,
有人可以帮助我我该怎么做,
thanks.
谢谢。
回答by clcto
Use integer division and the modulus operator:
使用整数除法和模运算符:
int input = 456789;
int a = input / 1000;
int b = input % 1000;
回答by pid
Splitting a "number" is not what you want to do. You want to split the NUMERAL, which is the string representing a quantity (number). You can split the numeral just like any other literal (string): don't parse the user's input and use substring. If you want to make sure the literal is an actual numeral, parse it to see if an exception is thrown. If not, you have a numeral. But don't hold on to the integers, keep the strings instead.
拆分“数字”不是您想要做的。您要拆分 NUMERAL,它是表示数量(数字)的字符串。您可以像任何其他文字(字符串)一样拆分数字:不要解析用户的输入并使用子字符串。如果要确保文字是实际数字,请对其进行解析以查看是否引发异常。如果没有,你有一个数字。但是不要保留整数,而是保留字符串。
回答by Dean Leitersdorf
Here you go:
干得好:
You give the method a number, start, and end indexes.
您为该方法指定一个编号、开始和结束索引。
public static void main(String[] args) {
System.out.println(getPartOfInt(93934934, 3, 7));`
}
public static int getPartOfInt(int number, int start, int end){
Integer i = new Integer(number);
char[] chars = i.toString().toCharArray();
String str = "";
for(int j = start; j < end && j < chars.length; j++){
str += chars[j];
}
return Integer.parseInt(str);
}
OR:
或者:
public static int getPartOfInt(int number, int start, int end){
String str = new Integer(number).toString();
return Integer.parseInt(str.substring(start, Math.min(end, str.length())));
}
回答by instanceof
You could also first convert the number to String, like so:
您也可以先将数字转换为字符串,如下所示:
int num = 456789;
String strNum = String.valueOf(num);
String part1 = strNum.substring(0, strNum.length() / 2 - 1);
String part2 = strNum.substring(strNum.length() / 2);
Then, you could convert it back to int, like so:
然后,您可以将其转换回 int,如下所示:
int part1Num = Integer.parseInt(part1);
int part2Num = Integer.parseInt(part2);
Now you can do all the arithmetic you want with those two int's.
现在你可以用这两个 int 做你想要的所有算术。
回答by Quest
@DeanLeitersdorf Here's the function ( @clcto s solution)
@DeanLeitersdorf 这是函数(@clcto 的解决方案)
int subinteger(int input, int from, int size)
{
while (input > pow(10, size + from) - 1)
input /= 10;
return input % (int)pow(10, size);
}
This is a c++ solution but i hope that you can convert it to Java easily
这是一个 C++ 解决方案,但我希望您可以轻松地将其转换为 Java