String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
I know how to use Delimiter but I just want to figure out why we write \\s* before and after the delimiter?
sonu raj wrote:I know how to use Delimiter but I just want to figure out why we write \\s* before and after the delimiter?
Because the useDelimiter() method takes a regular expression (regex) as its argument, and in a regex "\\s" means "a whitespace character". The '*' means "0 or more", so I would say that the regex is actually wrong; it should be "\\s+" ('+' means 1 or more).
PS: For more information, have a look at the documentation for java.util.regex.Pattern (←click), or look at the Java Tutorials.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
I think there is nothing wrong with that delimiter. It is simply different from \\s+fish\\s+
Try thisTry changing the \\s* to \\s+ or \\s and see the difference. I missed out a space or two so as to accentuate the difference. I usually warn against using \r and \n in Strings, but have used them here because they are kinds of whitespace.
Any Irishman can tell you why I called the Scanner murphy