简体   繁体   English

如何将带有数字和字母的字符串分成数字?

[英]How to separate a String with digits and letters into just digits?

public static String carRentalCode(String licensePlate) {
    // precondition:    licensePlate is a valid plate as described in pdf
    // postcondition:   return the car rental code for the licensePlate as described
    int values = 0;
    int ascii = 0;
    int sum = 0;
    int convert = 0;
    int count = 0;
    int a = 0;
    char letter;
    String license;
    char[] ch = new char[licensePlate.length()];
    for (int i = 0; i < licensePlate.length(); i++) { 
        ch[i] = licensePlate.charAt(i); 
    }
    for(int i = 0; i< ch.length; i++)
    {
        if(Character.isDigit(ch[i]))
        {
            values += Character.getNumericValue(ch[i]);
        }
        if(Character.isAlphabetic(ch[i]))
        {
            ascii += (int) ch[i];
            count++;
        }
    }
    sum = values + ascii;
    convert = sum%26 + 65;
    letter = (char)convert;
    String[] letters = licensePlate.split("\\d+");
    String lowercase = Arrays.toString(letters).toLowerCase();
    String code = Integer.toString(sum);
    return letter + code + lowercase;

Here I am supposed to return a car rental code based on a given license plate (the tester plate is "123ABC456" ).在这里,我应该根据给定的车牌(测试车牌是"123ABC456" )返回汽车租赁代码。 The only problem I have left is that my lowercase returns as [[,abc]] as opposed to the correct answer which is [abc] .我剩下的唯一问题是我的小写字母返回[[,abc]]而不是正确答案[abc]

How do you fix the empty space in front, and there being two pairs of brackets instead of one?前面的空位,原来是两对括号而不是一对,怎么解决? Or is there any other way to only obtain the letters of a string (ABC) , separated from the digits (123 and 456) and return that instead?或者有没有其他方法可以只获取字符串(ABC)的字母,与数字(123 和 456)分开并返回?

Expected: L219[abc]预期: L219[abc]

Actual: L219[[, abc]]实际: L219[[, abc]]

I changed some of your code, use String.toCharArray() to create an array of characters instead of copying it char by char, and replace all your digits by empty string to get only the alphabetic characters我更改了您的一些代码,使用String.toCharArray()创建一个字符数组,而不是逐字符复制它,并用空字符串替换所有数字以仅获取字母字符

public static String carRentalCode(String licensePlate) {
    // precondition:    licensePlate is a valid plate as described in pdf
    // postcondition:   return the car rental code for the licensePlate as described
    int values = 0;
    int ascii = 0;
    int sum = 0;
    int convert = 0;
    char letter;

    char[] ch = licensePlate.toCharArray();

    for (int i = 0; i < ch.length; i++) {
        if (Character.isDigit(ch[i])) {
            values += Character.getNumericValue(ch[i]);
        }
        if (Character.isAlphabetic(ch[i])) {
            ascii += ch[i];
        }
    }
    sum = values + ascii;
    convert = sum % 26 + 65;
    letter = (char) convert;

    String lowercase = licensePlate.replaceAll("\\d+", "").trim().toLowerCase();
    String code = Integer.toString(sum);
    return letter + code + "[" + lowercase + "]";
}

I just wrote the following code which extracts a word (without a number) from a string:我刚刚编写了以下代码,它从字符串中提取一个单词(没有数字):

import java.util.*;
import java.lang.*;
import java.io.*;

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

class Ideone
{
    static void extractWord(String str) 
    { 
        String regex = "[a-zA-Z]+"; 

        // compiling regex 
        Pattern p = Pattern.compile(regex); 

        // Matcher object 
        Matcher m = p.matcher(str); 

        while(m.find()) 
        { 
            System.out.println(m.group());
        } 
    } 

    public static void main (String[] args) 
    { 
        String str = "123ABC456"; 

        extractWord(str); 
    }
}

Hope this helps you.希望这对你有帮助。

Just as an example of a more functional approach:作为一个更实用的方法的例子:

String lowercase = licensePlate.chars()
                .filter(Character::isAlphabetic)
                .map(Character::toLowerCase)
                .mapToObj(c -> Character.toString((char) c))
                .reduce((a, b) -> a + b)
                .orElse("");

刚回来怎么样

letter + code + "[" + letters[1].toLowerCase() + "]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM