fuzzylogical fuzzylogical - 1 year ago 87
Scala Question

What is a more Scala way of writing this code?

I want to strip off the word "America/" from the start of each item in the list, and the code below does just that, but I feel like it can be done in a significantly better way.

var tz = java.util.TimeZone.getAvailableIDs
for(i <- 0 until tz.length) {
if(tz(i).startsWith("America/")) {
tz(i) = tz(i).replaceFirst("America/", "")
}
}

Answer Source

Simple and straight forward:

val tz = java.util.TimeZone.getAvailableIDs.map(_.replaceFirst("^America/", ""))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download