>
>
> Hi,
>
> Thank you for your feedback and guidance earlier.
>
> I’d like to propose a simple but practical method for inclusion in
> StringUtils:
>
>     public static List<Integer> findAllOccurrences(String str, String
> subStr)
>
> This method returns a list of all indices at which a substring appears in
> a given string.
>
> ---
>
> Real-World Use Cases:
>
> This method is frequently useful in:
>
> - Search & highlight features (in text editors, IDEs, or log viewers)
> - Syntax highlighting (e.g., highlighting all keywords in code)
> - Text annotation tools (especially in NLP, to label named entities)
> - Log analysis tools (to find all occurrences of “ERROR” or “WARN”)
> - Diff viewers or content comparison tools (to detect and mark repeated
> patterns)
>
> For example, in a JavaFX-based editor, we needed to highlight every match
> of a user-input term:
>
>     int index = text.indexOf(subStr);
>     while (index >= 0) {
>         list.add(index);
>         index = text.indexOf(subStr, index + 1);
>     }
>
> This logic is commonly repeated in different projects, and it’s easy to
> make mistakes with offsets or null checks. A reusable method in StringUtils
> would improve readability and correctness.
>
> ---
>
> I’ve reviewed both Commons Lang and Commons Text. Neither includes such a
> method to return all positions of a substring. The current
> indexOf/lastIndexOf methods only return the first or last match.
>
> If the community supports the idea, I’d be happy to raise a JIRA ticket
> and submit a patch with complete unit tests.
>
> Looking forward to your thoughts.
>
> Best regards,
> Kunal Bhangale
>

Reply via email to