Groovy: Use findAll to fetch string from long string.


How to use findAll() in groovy, when you have to find some specific string from string.

def name= 'this#   is to%% te$st ^in*d~~ all $$from CCa s<>>ng, this is basi?><:"{}cally fetch only interested thing.'
def name1 = ''
def findName = name.findAll(/[A-Za-z\s+]/).each{name1 += "${it}"}
println findName
println name1

We can also use

def findName = name.findAll(/[A-Za-z\s+]/)
def name1 = findName.join('')

This code will fetch only sting and space and give output in name1.

Output –

run-single:
[t, h, i, s,  ,  ,  , i, s,  , t, o,  , t, e, s, t,  , i, n, d,  , a, l, l,  , f, r, o, m,  , C, C, a,  , s, t, i, n, g,  , t, h, i, s,  , i, s,  , b, a, s, i, c, a, l, l, y,  , f, e, t, c, h,  , o, n, l, y,  , i, n, t, e, r, e, s, t, e, d,  , t, h, i, n, g]
this   is to test ind all from CCa sting this is basically fetch only interested thing
BUILD SUCCESSFUL (total time: 1 second)

Program has many ways to do.
Happy Programming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: