groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bahman Movaqar <Bah...@BahmanM.com>
Subject Re: String to Array of Substrings by delimiter
Date Fri, 26 Jun 2015 16:08:14 GMT
I'd go for this:
   
    def subs = s.split('/')
    (1..subs.size()-1).collect { subs[0..it].join('/') }

I don't like the version with a `for` (scanning through the string);
it's not beautiful.

--
Bahman Movaqar

http://BahmanM.com - https://twitter.com/bahman__m
https://github.com/bahmanm - https://gist.github.com/bahmanm
PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com)

On 06/26/2015 08:04 PM, Steve Amerige wrote:
> Hi all,
>
> Suppose you have:
>
>     String s = '/a/b/c/d'     // guaranteed to begin with a / and have
> at least one substring sequence after the /; can have more than 4 as
> shown in this example
>
> and I want the result:
>
>     [ '/a', '/a/b', '/a/b/c', '/a/b/c/d' ]
>
> where '/' can be any single character delimiter.  What would be the
> easiest, grooviest way to get that result?
>
> Thanks,
> Steve Amerige
> Principal Software Developer, Fraud and Compliance Solutions Development
> SAS Institute, 100 SAS Campus Dr, Room U3050, Cary, NC 27513-8617




Mime
View raw message