qben
qben

Reputation: 833

Scanner with input and delimiter

I would like to create a Scanner with a String, and I was just wondering if there's a constructor / static factory method to do this in one line.

So far the only way I found is this:

Scanner sc = new Scanner(inputString);
sc.useDelimiter(Pattern.compile("\\D"));

Is there a simpler way?

Upvotes: 1

Views: 72

Answers (1)

Tunaki
Tunaki

Reputation: 137184

You could do it in a single line:

Scanner sc = new Scanner(inputString).useDelimiter(Pattern.compile("\\D"));

useDelimiter returns this Scanner so you can use it to chain invocation.

If you find yourself doing this often, you can build your own static factory for this, and reuse it.

Upvotes: 4

Related Questions