简体   繁体   English

使用Java提取数字或单词的逗号分隔列表

[英]Extract comma separated list of number or words using Java

for example with the given input message: hello java 2017 ** 2020 world 例如,使用给定的输入消息: hello java 2017 ** 2020 world

  • When choice == 2 output 2017 , 2020 当选择== 2时2017 , 2020
  • when choice == 3 output hello , java , world 当choice == 3输出hello , java , world

Here is my program: 这是我的程序:

public static void print(String message, int choice){
    {    
        for (int i = 0; i < message.length();) {
            if (message.indexOf(" ") != -1) {
                System.out.print(message.substring(0, message.indexOf(" ")));
                message = message.substring(message.indexOf(" ") + 1);
                switch (choice) {
                    case 2: {
                        char c;
                        for (int k = 0; k < message.length(); k++) {
                            c = message.charAt(k);
                            if (Character.isDigit(c)) {
                                System.out.print(c+" , ");
                            }break;
                        }
                    }
                    case 3: {    
                        for (int z = 0; z < message.length(); z++) {
                            char tr = message.charAt(z);
                            if (Character.isAlphabetic(tr)) {

                                System.out.print(tr);
                                break;
                            }
                        }
                    }
                }
            } else {
                System.out.println(message);
                break;
            }
        }
    }
}

I want to do it without using split , replace , arrays or a tokenizer . 我想做到这一点而无需使用splitreplacearraystokenizer

Regex? 正则表达式?

You can match numbers with (\\\\d+) in Java and words (assuming from your example that it will contain only alphabetical characters with ([a-zA-z]+) 您可以将Java中的(\\\\d+)与数字和单词进行匹配(假设您的示例中仅包含([a-zA-z]+)字母字符,

Now you can go ahead and do the following: 现在,您可以继续执行以下操作:

public static void print(String message, int choice) {
    switch (choice) {
        case 2:
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(message);
            while (m.find()) {
                System.out.println(m.group());
            }
            break;
        case 3:
            Pattern p2 = Pattern.compile("([a-zA-z]+)");
            Matcher m2 = p2.matcher(message);
            while (m2.find()) {
                System.out.println(m2.group());
            }
            break;
    }
}

Instead of printing the groups, you can as well add them to a list for later use, etc. 您也可以将它们添加到列表中以供以后使用,而不是打印组。

However I suggest (as others did in the comments before) that you use the probably easier and faster tools Java already offers to you. 但是,我建议(就像其他人在之前的评论中所做的那样)建议您使用Java已经提供给您的可能更简单,更快的工具。

Testing the above code with your example like this 用您的示例测试上述代码

public static void main(String[] args) {
    String s = "hello java 2017 ** 2020 world";
    print(s, 2);
    System.out.println();
    print(s, 3);
}

prints: 打印:

2017 2017年
2020 2020

hello 你好
java java的
world 世界

Again you can put those in a List or other type of collection for later use including the comma separation. 同样,您可以将它们放入列表或其他类型的集合中,以供以后使用,包括逗号分隔。

暂无
暂无

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

相关问题 使用正则表达式从 java 中的逗号分隔字符串中提取特定单词 - extract specific word from comma separated string in java using regex 使用 java 8 将逗号分隔的字符串列表映射到列表 - List of comma separated String to Map of list using java 8 如何在 Java 程序中的 for 循环中存储和打印以逗号分隔的行号,我正在使用 Java 4,在 Java 4 中寻找 List 的替代方案 - how to store and print row number separated by comma in a for loop in a Java Program, I am using Java 4,looking for alternatives of List in Java 4 Java提取字符串中用逗号分隔的元素 - java extract elements separated by comma in a String 使用 Java Collections 和 Streams 拆分逗号分隔列表 - Splitting comma separated list using Java Collections and Streams Java 8:使用 Stream.of() 将逗号分隔的字符串转换为通用列表 - Java 8 : Comma separated string to Generic List using Stream.of() 如何将逗号分隔和双引号的单词字符串转换为 Java 中的字符串列表/数组 - How to convert comma separated and double quoted words string to list/array of strings in Java java regex匹配字符串,其中包含没有数字的单词,并且可以选择用逗号分隔 - java regex match string containing words with no digits and optionally separated by comma 使用StringBuilder生成逗号分隔的列表 - Produce a comma separated list using StringBuilder 使用java流获取逗号分隔的字符串 - get a comma separated string using java stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM