Splitting a string between letters and digits

0 votes

I have the following String:

String str = "123abc345def";

Now want to divide the above String as shown below, but somehow I am unable to perform this.

s[0] = "123";
s[1] = "abc";
s[2] = "345";
s[3] = "def";

Please help me out of these. Thanks in advance.

Oct 25, 2018 in Java by v.liyyah
• 1,300 points
4,316 views

2 answers to this question.

0 votes

Maybe you can try splitting the String with (?<=\D)(?=\d)|(?<=\d)(?=\D).

You can use it as:

str.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

This will compare the positions between a number and NaN be it any order and split the String accordingly.

answered Oct 25, 2018 by code.reaper12
• 3,500 points
0 votes
answered Sep 17, 2020 by Himanshu

Related Questions In Java

0 votes
1 answer

Difference between String.equals() and ==

Though both are used for comparison, but the ...READ MORE

answered May 7, 2018 in Java by geek.erkami
• 2,680 points
864 views
0 votes
1 answer
0 votes
2 answers

How to check if a string has only letters

You could probably use Java 8 lambda ...READ MORE

answered Aug 30, 2019 in Java by Karan
• 19,610 points
3,202 views
0 votes
2 answers

What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association?

JPA mapping annotation can be classified as ...READ MORE

answered Oct 28, 2020 in Java by bjjj
• 140 points
21,199 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,620 views
0 votes
2 answers

Generate an alpha-numeric string randomly

Java supplies a way of doing this ...READ MORE

answered Jul 18, 2018 in Java by Daisy
• 8,140 points
2,521 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,121 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,920 points
2,680 views
0 votes
1 answer

How to remove ‘char’ from a String?

Hi...here you can try using the overloaded ...READ MORE

answered May 22, 2018 in Java by code.reaper12
• 3,500 points
662 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,500 points
4,648 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP