简体   繁体   English

访问字符串中的单个字母/数字中的数字-Java

[英]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 : 如果要通过索引访问数字而不转换为字符串,则可以使用这两个函数lengthdigitAt

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.

相关问题 Java-将字母字符串转换为数字字符串 - Java - convert string of letters to a string of digits JAVA -我如何编写带有字母和数字的转换(小写-&gt;大写,单个数字-&gt;其值的两倍,两位数字-&gt;其数字总和)程序? - JAVA -How do I write a converting (lowercase->uppercase, single digits->double its value, two digits->sum of its digits) program w/ letters & numbers? 在Java中的字符串中拆分字母和数字 - Splitting letters and numbers in a string in java 检查字符串是否由 3 个大写字母和 4 个数字组成(在 JAVA 中) - Checking if a string is composed of 3 capital letters and 4 numbers (in JAVA) Java字符串:检查连续的字母或数字 - Java String: Checking for consecutive letters or numbers Java方法在字符串中查找大写字母和数字 - Java Method to find uppercase letters and numbers in a String 检查 java 中的密码。 它必须包含至少 10 个字母(数字或数字)和至少 2 个数字 - Checking Password in java. It must contains at least 10 letters (numbers or digits) and at least 2 digits (java.util.regex.Pattern的问题)检查字符串的数字和字母 - (Problems with java.util.regex.Pattern) checking string for digits and letters Java计算字符串中数字,特殊字符和字母的更改次数 - Java count the number of changes in Digits, special characters and letters in a string Java 正则表达式中字符串的单个数字 - Single Digits From String In Java Regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM