Groovy GStrings and numberformat -


i'm writing script in groovy process files , have following method create resulting filename

static string formatfilename(string prefix, int counter, string extension) {     string counters = string.format('%04d', counter)     return "$prefix-$counters$extension" } 

is there more elegant way of formatting counter in gstring?

frankly, there's not can here. making shorter nitpicking.

as mentioned in tim_yates' comment, make one-liner

return "$prefix-${string.format('%04d', counter)}$extension" 

i can think of 1 way make shorter, give on gstrings , use sprintf instead, example of groovy goodness. namely, extension method of object class. personally, find easier read braces mashup.

return sprintf("%s-%04d%s", prefix, counter, extension) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -