[英]Accessing single letters in String / digits in numbers - Java
I have numeric input (11 digits), and I need to perform some operations on each digit (example: multiply 1st by 5, 2nd by 3, etc.). 我有数字输入(11位数字),我需要对每个数字执行一些操作(例如:将1st乘以5、2nd乘以3等)。 How can I do so in Java?
如何在Java中这样做? Is there a simple way to access single letter / digit?
是否有一种简单的方法来访问单个字母/数字? Is there another way to do it?
还有另一种方法吗?
If you don't want to convert the number to a string, then there is a simple trick. 如果您不想将数字转换为字符串,则有一个简单的技巧。
digit = number % 10
will give you the right most digit. 将为您提供最正确的数字。
number = number / 10
Will remove the right most digit from the number. 从数字中删除最右边的数字。
So you can run in a loop until the number reaches 0. 因此,您可以循环运行直到数字达到0。
while(0 < number)
{
int digit = number % 10;
number = number / 10;
// do here an operation on the digits
}
You can use a for loop to help you count. 您可以使用for循环来帮助您计数。 For example
例如
for(int index = 0; 0 < number; ++index, number /= 10)
{
int digit = number % 10;
// do an operation on the number according to the 'index' variable
}
Here is a similar StackOverFlow question on a similar question 这是关于类似问题的类似StackOverFlow问题
You can use .charAt()
to get a character from a string. 您可以使用
.charAt()
从字符串中获取字符。 Then using Character.getNumericValue()
you can convert the character to an integer. 然后,使用
Character.getNumericValue()
可以将字符转换为整数。
Like this: 像这样:
String string = "1434347633";
int digit1 = Character.getNumericValue(string.charAt(1));
Convert that number input to String
data type so that you can interpret it as a String. 将该数字输入转换为
String
数据类型,以便可以将其解释为String。
int numbers = 1122334455; // integer won't be able to save this much data,
// just for example ok,
String numberString = numbers.toString();
foreach (char number in numberString) {
// do work on each of the single character in the string.
}
You should work them out, depending on the some condition. 您应该根据某些情况来解决它们。
If you want to access the digits by index without converting to a string, you can use these two functions length
and digitAt
: 如果要通过索引访问数字而不转换为字符串,则可以使用这两个函数
length
和digitAt
:
public class DigitAt {
public static int length(long v) {
return (int) Math.ceil(Math.log10(v));
}
public static int digitAt(long v, int digit) {
return (int) ((v / (long) Math.pow(10, length(v) - digit - 1)) % 10);
}
public static void main(String[] args) {
System.out.println("Digits: " + length(1234567));
System.out.println(digitAt(1234567, 0));
System.out.println(digitAt(1234567, 1));
System.out.println(digitAt(1234567, 6));
}
}
Well there are many ways you can do it like : 好吧,有很多方法可以做到:
int a = 12345;
int b;
while(a>0)
{
b = a%10;
System.out.print(b + " ");
a = a/10;
}
Here it gives you the digits in reverse order like you will get b=5 then b=4.... 在这里它给您的数字是相反的顺序,就像您将得到b = 5然后b = 4 ....
You can just manipulate them 您可以操纵它们
Other way 另一种方式
int d = 12345;
String str = String.valueOf(d);
for(int i=0;i<str.length();i++)
{
char c = str.charAt(i);
System.out.print(Character.getNumericValue(c) * 10 + " ");
}
Or 要么
char c[] = str.toCharArray();
for(Character ch : c)
{
System.out.print(Character.getNumericValue(ch) * 2 + " ");
}
public String stringAfterOperations(String digits) {
ArrayList<Integer> z = new ArrayList<Integer>();
for(Character c: digits.toCharArray()) {
z.add(Character.getNumericValue(c));
}
//TODO Set your own operations inside this "for"
for(int i=0; i<z.size(); i++){
if(i == 1){
z.set(i, z.get(i)*4);
}
else if(i == 7){
z.set(i, z.get(i)/3);
}
else {
z.set(i, z.get(i)+2);
}
}
String newString = "";
for(Integer i: z){
newString += i;
}
return newString;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.