println args
println args.size()
println args.each{arg-> println arg}
println args.class
if (args.contains("Hello"))
println "Found Hello"
when ran give following error:
[hello, somethingelse]
2
hello
somethingelse
[hello, somethingelse]
class [Ljava.lang.String;
Caught: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.
String;.contains() is applicable for argument types: (java.lang.String) values:
[Hello]
why can I not do contains
?
Groovy - contains() Checks if a range contains a specific value.
The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.
Groovy - Lists contains() Returns true if this List contains the specified value.
Because args
is String[]
but not List<String>
You can use
if (args.grep('Hello'))
println "Found Hello"
That's because args is an array of String (just like in Java) and not a String, take a look at the result of:
print args.getClass()
>>class [Ljava.lang.String;
Notice the [L notation.
A regular String would result in:
>>class java.lang.String
The Groovy containers do not have the contains() operation (String does), yet the java.lang.Object of Groovy SDK has the grep() operation (shown on the first reply).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With