-2

For a string containing multiple characters in the beginning and multiple numeric at the end, how to separate the string in two parts, first part to be stored as a String and the second part to be stored as an int.

The Strings are like DRR2110012, SRR211001, ABCDEFG1, and i want to separate the ASCII part and store it in another String variable and the digits stored in another int variable for each string using Java.

I know I can split a string like this: array = "1,2,3,4".split(','); But this doesn't help since I don't have a separator.

4
  • split takes regex Commented Jul 20, 2017 at 7:57
  • You should look at regex Commented Jul 20, 2017 at 7:57
  • You need to use split and then cast the part you want to integer. Commented Jul 20, 2017 at 8:00
  • @SadiqAli what would you split on? Commented Jul 20, 2017 at 8:12

1 Answer 1

1

You can split using regex in the following way.

String str = "DRR2110012";
String[] part = str.split("(?<=\\D)(?=\\d)");
System.out.println(part[0]);
System.out.println(part[1]);
Sign up to request clarification or add additional context in comments.

1 Comment

If a string like "ABC123DCE" then this regex will not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.