Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 879567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:57:12+00:00 2026-05-15T11:57:12+00:00

Java’s string split(regex) function splits at all instances of the regex. Python’s partition function

  • 0

Java’s string split(regex) function splits at all instances of the regex. Python’s partition function only splits at the first instance of the given separator, and returns a tuple of {left,separator,right}.

How do I achieve what partition does in Java?

e.g.

"foo bar hello world".partition(" ")

should become

"foo", " ", "bar hello world"
  • Is there an external library which
    provides this utility already?

  • how would I achieve it without
    an external library?

  • And can it be achieved without an external library and without Regex?

NB. I’m not looking for split(” “,2) as it doesn’t return the separator character.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:57:12+00:00Added an answer on May 15, 2026 at 11:57 am

    The String.split(String regex, int limit) is close to what you want. From the documentation:

    The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

    • If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array’s length will be no greater than n, and the array’s last entry will contain all input beyond the last matched delimiter.
    • If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
      • If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

    Here’s an example to show these differences (as seen on ideone.com):

    static void dump(String[] ss) {
        for (String s: ss) {
            System.out.print("[" + s + "]");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        String text = "a-b-c-d---";
    
        dump(text.split("-"));
        // prints "[a][b][c][d]"
    
        dump(text.split("-", 2));
        // prints "[a][b-c-d---]"
    
        dump(text.split("-", -1));
        // [a][b][c][d][][][]
        
    }
    

    A partition that keeps the delimiter

    If you need a similar functionality to the partition, and you also want to get the delimiter string that was matched by an arbitrary pattern, you can use Matcher, then taking substring at appropriate indices.

    Here’s an example (as seen on ideone.com):

    static String[] partition(String s, String regex) {
        Matcher m = Pattern.compile(regex).matcher(s);
        if (m.find()) {
            return new String[] {
                s.substring(0, m.start()),
                m.group(),
                s.substring(m.end()),
            };
        } else {
            throw new NoSuchElementException("Can't partition!");
        }
    }
    public static void main(String[] args) {
        dump(partition("james007bond111", "\\d+"));
        // prints "[james][007][bond111]"
    }
    

    The regex \d+ of course is any digit character (\d) repeated one-or-more times (+).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 431k
  • Answers 431k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assuming the div doesn't have divs that are siblings to… May 15, 2026 at 2:25 pm
  • Editorial Team
    Editorial Team added an answer Are you aware of the EXPLAIN statement? This command displays… May 15, 2026 at 2:24 pm
  • Editorial Team
    Editorial Team added an answer Could not find a solution. Worked around this behaviour by… May 15, 2026 at 2:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.