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 852533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:36:47+00:00 2026-05-15T07:36:47+00:00

I have a string which contains a contiguous chunk of digits and then a

  • 0

I have a string which contains a contiguous chunk of digits and then a contiguous chunk of characters. I need to split them into two parts (one integer part, and one string).

I tried using String.split("\\D", 1), but it is eating up first character.
I checked all the String API and didn’t find a suitable method.

Is there any method for doing this thing?

  • 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-15T07:36:48+00:00Added an answer on May 15, 2026 at 7:36 am

    Use lookarounds: str.split("(?<=\\d)(?=\\D)")

    String[] parts = "123XYZ".split("(?<=\\d)(?=\\D)");
    System.out.println(parts[0] + "-" + parts[1]);
    // prints "123-XYZ"
    

    \d is the character class for digits; \D is its negation. So this zero-matching assertion matches the position where the preceding character is a digit (?<=\d), and the following character is a non-digit (?=\D).

    References

    • regular-expressions.info/Lookarounds and Character Class

    Related questions

    • Java split is eating my characters.
    • Is there a way to split strings with String.split() and include the delimiters?

    Alternate solution using limited split

    The following also works:

        String[] parts = "123XYZ".split("(?=\\D)", 2);
        System.out.println(parts[0] + "-" + parts[1]);
    

    This splits just before we see a non-digit. This is much closer to your original solution, except that since it doesn’t actually match the non-digit character, it doesn’t “eat it up”. Also, it uses limit of 2, which is really what you want here.

    API links

    • String.split(String regex, int limit)
      • 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string which contains 3 elements: a 3 digit code (example: SIN,
Hello I have a string which contains a mail address. For example ( user@foo.bar.com
Let's say I have a string which contains a Unix-style local path to a
hey guys, i have a string which contains say 100 words, now i want
I have an std::string which contains 3 numbers which are separated by spaces for
I've a string which contains many fields I'd like to extract from it. These
I have a string which sometimes gives character value and sometimes gives integer value.
I have HTML string which is created using an xml file by a third
Possible Duplicate: Is There A Built-In Way to Split Strings In C++? i have
What I want to do is read a line from text file which contains

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.