在Java中将数字从字符串转换为文本

时间:2017-09-07 03:02:27

标签: java

Java代码的以下部分扫描给定的字符串以查找单个或分组的数字序列。但我发现的问题是我需要将数字的名称写为个体而不是分组。也就是说,如果字符串有"Hello78, my name is number07 and not number1",而不是打印为"Seventy Eight",我怎样才能将其设为"Seven Eight"

    Pattern patternString = Pattern.compile("\\d+");
    String inputText = "32h28ello12'34' this is a test89 87";
    Matcher findNumber = patternString.matcher(inputText);
    Scanner findText = new Scanner(inputText);
    findText.useDelimiter("[^\\p{Alnum},\\.-]");
    int counter = 0;
    List<String> list = new ArrayList<>();

    while (findNumber.find()) {
        list.add(findNumber.group());

        System.out.println(findNumber.group(0));
    }

所以,这是一个输出的例子,只是为了引导自己:

32
28
12
34
89
87
-----------------------------------------------
LIST: [32, 28, 12, 34, 89, 87]
32h28ello12'34' this is a test89 87
-----------------------------------------------

我使用下面的行继续测试并替换文本的数字,但后来我意识到我需要替换该位置的整数。也就是说,如果它是30,则文本仅为"Three Zero"而不是"Three"或仅"Zero"

numString = inputText.replace(Character.toString(inputText.charAt(atPos)), one);

我所拥有的一些想法是,例如,将每个输出数字放在另一个变量中,将其读回,然后将其拆分(每个)并写入数字的名称,但我无法弄清楚如何做到这一点。任何想法都将不胜感激。

3 个答案:

答案 0 :(得分:2)

如果你希望"HelloSeven Eight, my name..."作为你的输出,那么你可以使用它:

    String[] numberTexts = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    StringBuilder builder = new StringBuilder();
    String inputText = "32h28ello12'34' this is a test89 87";

    for (int i=0; i < inputText.length(); i++) {
        char c = inputText.charAt(i);
        if (Character.isDigit(c)) {
            String numberText = numberTexts[Character.getNumericValue(c)];
            builder.append(numberText);
            // append space if next character is a digit
            if (i < inputText.length() - 1 && Character.isDigit(inputText.charAt(i+1))){
                builder.append(" ");
            }
        } else {
            builder.append(c);
        }
    }

    System.out.println(builder.toString());

这里对所有字符迭代inputText,并用其文本替换数字。如果下一个字符是数字,则附加空格字符。

答案 1 :(得分:1)

使用StringBuilder而不是不断创建新的中间字符串。

StringBuilder sb = new StringBuilder();
for ( char c : inputText ) {
  switch (c) {
    case '0':
       sb.append("zero ");
       break;
    // similar for '1 thru '9'
    default:
       sb.append(c);
       break;
  }
}
String output = sb.toString();

答案 2 :(得分:1)

使用Map<Character, String>

    Map<Character, String> map = new HashMap<>();
    map.put('0', "zero");
    map.put('1', "one");
    map.put('2', "two");
    map.put('3', "three");
    map.put('4', "four");
    map.put('5', "five");
    map.put('6', "six");
    map.put('7', "seven");
    map.put('8', "eight");
    map.put('9', "nine");

    String inputString = "Hello78, my name is number07 and not number1";

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < inputString.length(); i++) {
        builder.append(map.getOrDefault(inputString.charAt(i), String.valueOf(inputString.charAt(i))));
    }

    String outputString = builder.toString();

修改 使用@Kevin Anderson在评论中建议的String[](这看起来好多了):

        String[] array = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

        String inputString = "Hello78, my name is number07 and not number1";

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < inputString.length(); i++) {
            if (Character.isDigit(inputString.charAt(i))) {
                builder.append(array[Character.digit(inputString.charAt(i), 10)]);
            } else {
                builder.append(inputString.charAt(i));
            }
        }

        String outputString = builder.toString();

outputString将在上述任一解决方案中都有Helloseveneight, my name is numberzeroseven and not numberone

您需要在单词的开头/结尾处理空格。