groovykatalon-studio

groovy: remove numbers and period sign from list


I have a following list:

def myOptions = ['1.0 Small', '2.0 Medium', '3.0 Large']

I want to remove numbers and period sign and just return the list as ['Small', 'Medium', 'Large']

I am trying the following but it still prints the original elements of list.

for (String el: myOptions) {
            StringUtils.remove(el, ".").replaceAll("[0-9]", "").trim();
            KeywordUtil.logInfo(el);
        }

Solution

  • Thousands ways:

    def myOptions = ['1.0 Small', '2.0 Medium', '3.0 Large']
    
    def resultSplit = myOptions*.split( ' ' )*.getAt( 1 )
    def resultReplace = myOptions*.replaceFirst( /\d+\.\d+ (\w+)/, '$1' )
    
    assert resultSplit == ['Small', 'Medium', 'Large']
    assert resultReplace == ['Small', 'Medium', 'Large']