Hello Yokesh, This functionality already exists in ArrayUtils (subarray()) and StringUtils (substring()).
Gary On Sun, Jul 27, 2025, 12:06 Yokesh Chowdary <bollineniyok...@gmail.com> wrote: > Hi All, > > I'd like to propose adding a new utility method slice() to Apache Commons > Lang that brings Python-style slicing capabilities to Java for Collections, > arrays (primitive and wrapper types), and String. > The proposed method would follow the form: *slice(input, start, end, step)* > > This would be a highly reusable utility that simplifies what is currently > verbose or error-prone logic in standard Java. It could live in a new class > like *SliceUtils *or be integrated into existing utility classes such > as *ArrayUtils > *and *StringUtils*. > > *Highlights:* > 1) Supports positive and negative indices for start,end and step > 2) Works on Collections, Arrays and Strings > > *Examples:* > List<String> names = Arrays.asList("Hello","Hi","Bye"); > SliceUtils.slice(names, 0,2); // returns ["Hello", "Hi"] > > String s = "abcdef"; > SliceUtils.slice(s, 1, 4); // returns "bcd" > > int[] numbers = {1, 2, 3, 4, 5, 6}; > SliceUtils.slice(numbers, 0, -1, 2); // returns [1, 3, 5] > > If the community agrees this is in scope for Commons Lang, I’d be happy to > implement the feature with full test coverage and documentation. > > Looking forward to your thoughts. > > Regards, > Yokesh > [bollineniyok...@gmail.com] >