I got some function:
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\n")
.shuffled()
.first()
And if I try to get or print some string with the 2 values obtained from this function, the first value disappears. For example:
println("${selectHometown() ${selectHometown() }")
Will only print one city name, while I expect two. I guess the problem is related to string concatenation in Kotlin. Of course, I can get the desired result in a different way, but I'm wondering why this one doesn't work.
Windows way of terminating a line is to use "\r\n" so use it as delimiter :
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\r\n")
.shuffled()
.first()
println("${selectHometown()} ${selectHometown()}")
I tried:
String test = "racecary";
StringBuilder stringBuilder = new StringBuilder(test);
System.out.println(stringBuilder.reverse()+" -------------");
if (stringBuilder.reverse().toString().equalsIgnoreCase(test)) {
System.out.println("Pass");
}else {
System.out.println("Fail");
}
It always prints pass, even when I mispelled racecar, but passes when I use == inplace of .equalsIgnoreCase.
Am I doing something wrong?
I have already gone through following but didnt get the answer.
JAVA .equalsIgnoreCase not working
JAVA .equalsIgnoreCase not working
StringBuilder.reverse() modifies the builder in place. The test printout is causing the string to be reversed twice, leaving it unmodified. Get rid of the printout and the code will work as expected.
I have a string /sample/data. When I split using split I get the following result,
["","sample","data"]
I want to ignore the empty string(s). So I tried the following code,
"/sample/data".split('/').findAll(it != "")
It gives me an error "cannot call String[] findAll with argument bool".
How can I split and get a List without empty string in it?
split method returns array.
If you need List, use tokenize
"/sample/data".tokenize('/')
also you don't need to use findAll in this case.
You can do as below:
println "/sample/data".split('/').findAll {it}
findAll {it} would fetch all the non empty values.
Parens would work (see comments on question). So your solution is already close:
"/a/b".split("/").findAll()
Because most of the Groovy functions have a zero arity, which will call the function with an identity closure. And since an empty string is considered falsey, this will filter them out.
I have a function with two parameters which accepts string and a list. I now need to pass a third String parameter into this function which defaults to an empty string. Heres my function :
Here is how I call the function:
rows("{CALL " + storedProc + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}",param1,param2,param3)
Here is the function
List<GroovyRowResult> rows(String query,java.lang.Object[] parameterMap){
......
}
Now, in the above function, the input parameters is as follows:
query = {CALL reportStoredProc(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}
parameterMap = [param1,param2,param3]
When I put a thrid default paramter in the function like:
List<GroovyRowResult> rows(String query,java.lang.Object[] parameterMap, String test=''){
......
}
if i pass "Hello" as test the string in test is taken as an elemtn in the list. So, now the parmeters will be:
query = {CALL reportStoredProc(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}
parameterMap = [param1,param2,param3,Hello]
test = ''
Is, there a workaround here to pass the third parameter without add it into the list? It might be a silly question but Im stuck with this for some time now.
Note: to call stored procedure I am compelled to use rows instead of call or execute. Also, I cant use [] to pass parameterMap while calling the function.
Also, the test must be the third parameter
Thanks,
I'm struggling to find examples of findAll with groovy. I've got a very
simple code snippet that gets the property of a node and outputs it's
value. Except I'm only getting the last value when I'm looping through
a series of properties. Is there something I'm doing wrong here, this
seems really simple.
JcrUtils.getChildNodes("footer").findAll{
selectFooterLabel = it.hasProperty("footerLabel") ? it.getProperty("footerLabel").getString() : ""
}
In my jsp I'm just printing the property:
<%=selectFooterLabel%>
Thanks for the help!
findAll returns a List containing all the items in the original list for which the closure returns a Groovy-true value (boolean true, non-empty string/map/collection, non-null anything else). It looks like you probably wanted collect
def footerLabels = JcrUtils.getChildNodes("footer").collect{
it.hasProperty("footerLabel") ? it.getProperty("footerLabel").getString() : ""
}
which will give you a List of the values returned by the closure. If you then want only the subset of those that are not empty you can use findAll() with no closure parameter, which gives you the subset of values from the list that are themselves Groovy-true
def footerLabels = JcrUtils.getChildNodes("footer").collect{
it.hasProperty("footerLabel") ? it.getProperty("footerLabel").getString() : ""
}.findAll()