使用 java Scanner 获取字符串中的令牌数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17566845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:21:07  来源:igfitidea点击:

Obtain number of token in a string with java Scanner

java

提问by alessandrob

Is there a way to obtain the number of token in a string obtained by Method Scanner in Java?

有没有办法在Java中获取Method Scanner获取的字符串中的令牌数?

I mean, i can use s = sc.nextLine()to obtain an input line as a string. But on this string I cannot use lenght()method cause it gives me the total number of characters (I think).

我的意思是,我可以s = sc.nextLine()用来获取输入行作为字符串。但是在这个字符串上我不能使用lenght()方法,因为它给了我字符总数(我认为)。

Are existing any standard methods to obtain the number of token? Thanks in advance

是否存在任何获取令牌数量的标准方法?提前致谢

回答by Achintya Jha

Try this:

试试这个:

int frequency = new StringTokenizer(myString, " ").countTokens();

For more details see StringTokenizer.

有关更多详细信息,请参阅StringTokenizer

回答by f1sh

Unfortunately, Scannercannot do token counting without consuming these tokens from the input. So in order to access those tokens, you have to save them in a list:

不幸的是,Scanner如果不从输入中消耗这些令牌,就无法进行令牌计数。因此,为了访问这些令牌,您必须将它们保存在一个列表中:

List<String> tokens = new LinkedList<String>();
Scanner sc = new Scanner(System.in);
int count = 0;
while(sc.hasNext()) {
  tokens.add(sc.next());
  count++;
}
System.out.println("Number of tokens: "+count);

回答by BaSsGaz

Use split(), it supports regex, unlike StringTokenizer.

使用split(),它支持正则表达式,不像StringTokenizer.

int nbOfTokens = sc.nextLine().split(sc.delimiter().pattern()).length;

回答by Thisuri

import java.io.*;
import java.util.*;
public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String delims= "[ .,'!?_@]+";
        int length1=s.split(delims).length;

        System.out.println(length1);
        String[] tokens=s.split(delims);
        for(String token : tokens){
            System.out.println(token);

        }


        scan.close();
    }
}

回答by Jean Logeart

You can use Matcher:

您可以使用匹配器

Pattern pattern = Pattern.compile(token);
Matcher matcher = pattern.matcher(s);
int count = 0;
// Check all occurrences
while (matcher.find()) {
    ++count;
}

回答by sp00m

You could manage it using the splitmethod:

您可以使用以下split方法管理它:

public static int getTokenCount(String input) {
    if (input == null) {
        return 0;
    }
    input = input.trim();
    if (input.isEmpty()) {
        return 0;
    }
    return input.split("\s+").length;
}