Hi Gary,

Thanks for the quick reply!

You're absolutely right, *ArrayUtils.subarray()* and *StringUtils.substring()
*covers basic slicing. However, my proposal aims to enhance these methods
by introducing:

1) Support for step
ArrayUtils.slice(new int[]{1, 2, 3, 4, 5, 6}, 0, -1, 2); // returns [1, 3,
5]

2) Support for negative indices
StringUtils.slice("abcdef", -4, -1); // returns "cde"

These enhancements bring Python-like expressiveness to Java, making slicing
more intuitive and concise for developers, especially when dealing with
dynamic ranges or reverse slicing.
Would love to hear thoughts on whether extending existing utils or
introducing a SliceUtils would be preferable.

Regards,
Yokesh

On Sun, Jul 27, 2025 at 10:02 PM Gary Gregory <garydgreg...@gmail.com>
wrote:

> 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]
> >
>

Reply via email to