Java – use the stringtokenizer to return the rest of the string

After watching it for more than an hour, I can't seem to find a way to achieve this

If I do

StringTokenizer sTokenizer = new StringTokenizer(expression);
//operations with some of the tokens here
System.out.println(sTokenizer.nextToken());

It will be separated by spaces

I tried

sTokenizer.nextToken(null)

But this just throws a null pointer exception

I know I can put some random strings in the parameters that are unlikely to appear in the expression, but this is not ideal

If I implement this with substrings

expression.substring(currentposition)

Will work, but I need a stringtokenizer

In short, I'm trying to find a way to retrieve the rest of the string (used by the string tokenizer) as a single tag

Solution

You can use stokenizer nextToken(“”). It's close to what you're looking for This returns the rest of the string as a single token containing a delimiter Example:

public static void main(String[] args){
    String expression = "a b c d e";
    StringTokenizer sTokenizer  = new StringTokenizer(expression);
    System.out.println(sTokenizer.nextToken());
    System.out.println(sTokenizer.nextToken(""));
}

Output:

a
 b c d e
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>