groovy
  1. groovy
  2. GROOVY-5331

Make List join method handle nulls/emptry strings

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      I would like List.join method to not include null and empty elements. Similar to apache StringUtils.join. Perhaps a new join method that takes a boolean could be added?

        Activity

        Hide
        David Donn added a comment -

        Actually, I was wrong about apache StringUtils. It doesn't remove null/whitespace elements.

        I guess what I'm asking for is something like this to be part of core groovy:

        List.metaClass.nullSafeJoin = { separator ->
        delegate.findAll

        { it && !it.isAllWhitespace() }

        .join(separator)
        }

        Show
        David Donn added a comment - Actually, I was wrong about apache StringUtils. It doesn't remove null/whitespace elements. I guess what I'm asking for is something like this to be part of core groovy: List.metaClass.nullSafeJoin = { separator -> delegate.findAll { it && !it.isAllWhitespace() } .join(separator) }
        Hide
        Guillaume Laforge added a comment -

        You can always use grep() and trim() to easily get what you want here:

        ​['a', null, '   ', 'b', '', 'c']*.trim().grep().join()​​​​​​​​​
        
        Show
        Guillaume Laforge added a comment - You can always use grep() and trim() to easily get what you want here: ​['a', null , ' ', 'b', '', 'c']*.trim().grep().join()​​​​​​​​​
        Hide
        Paul King added a comment -

        -1, I too think you can get what is required by using other methods in combination

        Show
        Paul King added a comment - -1, I too think you can get what is required by using other methods in combination
        Hide
        David Donn added a comment -

        Thanks for your suggestion. I think you are probably right and that there's not enough justification for a totally new method for Lists so I am closing this issue.

        Show
        David Donn added a comment - Thanks for your suggestion. I think you are probably right and that there's not enough justification for a totally new method for Lists so I am closing this issue.

          People

          • Assignee:
            Unassigned
            Reporter:
            David Donn
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: