Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1037

Join list of objects with different end separator

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: Discussion
    • Component/s: lang.*
    • Labels:
      None

      Description

      A common programming challenge is joining a list with a different separator between the final two items. For example, joining a list with ", " as the primary separator but " and " as the final separator. Take a look at this Stack Overflow question to see just how awkward the current solutions are.

      I propose that we extend StringUtils with three additional methods:

      public static String join(Iterable<?> iterable, String separator, String endSeparator)
      
      public static String join(Object[] array, String separator, String endSeparator)
      
      public static String join(Object[] array, String separator, String endSeparator, int startIndex, int endIndex)
      

      This could also be added to the other join methods for primitives, but I suspect the primary use case will be strings and I don't suggest we pollute the class any further.

      Below shows the expected pseudo output using ", " and " and ":

      join(a) == a
      join(a,b) == a and b
      join(a,b,c) == a, b and c
      

        Attachments

          Activity

            People

            • Assignee:
              dmjones500 Duncan Jones
              Reporter:
              dmjones500 Duncan Jones
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: